我试图在项目中进行测试,但从mongodb中得到了一些错误。 我在登录网站的功能中使用测试,该功能检查用户是否在数据库中,然后检查密码是否正确。 但是,当我使用测试时,他不知道数据库,如何在他连接到我的真实mongodb的测试中做到这一点? 这是我的代码:
var expect = require('expect');
const MongoClient = require('mongodb').MongoClient;
const app = require("../app"); // Link to your server file
const supertest = require("supertest");
const request = supertest(app);
const manager = require("../app/server/modules/account-manager");
var db, accounts, phrases, categories;
describe("Mongo DataBase Manager", () => {
beforeAll(async () => { // here i tried to connect to my database
MongoClient.connect(process.env.DB_URL, { useUnifiedTopology: true, useNewUrlParser: true }, function(e, client) {
if (e) {
console.log(e);
} else {
db = client.db(process.env.DB_NAME);
accounts = db.collection('accounts');
phrases = db.collection('pharses');
categories = db.collection('categories');
}
});
});
test("swisa is in the accounts database", () => {
const e = manager.manualLogin("swisa", "123456");
expect(e).toBe(null);
});
});
这是手动登录功能:
exports.manualLogin = function(user, pass, callback)
{
accounts.findOne({user:user}, function(e, o) {
if (o == null){
callback('user-not-found');
} else{
validatePassword(pass, o.pass, function(err, res) {
if (res){
callback(null, o);
} else{
callback('invalid-password');
}
});
}
});
}
,错误是:
TypeError:无法读取未定义的属性'findOne'
201 | exports.manualLogin = function(user, pass, callback)
202 | {
> 203 | accounts.findOne({user:user}, function(e, o) {
感谢帮助。