Tho*_*mas 5 testing mongodb node.js
我的问题有两个层面。
findOneMongoDB 中的对象转换为真实对象?(以下问题示例)insertOne)后验证 MongoDB 数据库内容的正确方法是什么?我只使用原始 MongoDB 驱动程序,没有使用 Mongoose。这是一个使用 TypeScript 和 Mocha 进行测试的 NodeJS 应用程序。
假设我有这个示例类:
export class Person {
get Name(): string{
return this.name;
}
set Name(value: string) {
this.name= value;
}
constructor(private name: String) {
}
}
Run Code Online (Sandbox Code Playgroud)
然后为了进行一些测试,我Person使用以下命令将 a 插入到 MongoDB 数据库中insertOne
db.collection.insertOne(new Person("Thomas"));
Run Code Online (Sandbox Code Playgroud)
现在,我想验证该人是否已正确插入,因此我使用findOne并验证这些功能。
const person: Person = await db.collection.findOne(collection, {-insert Person query-}) as Person;
// Here is what I want to fix (this errors out):
expect(person.Name).to.equal("Thomas");
Run Code Online (Sandbox Code Playgroud)
在这里,该 person 作为 json 对象返回(这是预期的)。这意味着该.Name属性不存在,仅存在.name。
再说一次,问题在帖子的顶部!谢谢你!