使用Mocha在NodeJS和MongoDB中测试用户模型时没有结果
问题描述:
我正在用mocha测试我的模型。我有以下文件:使用Mocha在NodeJS和MongoDB中测试用户模型时没有结果
test/utils.js
'use strict';
// Modified from https://github.com/elliotf/mocha-mongoose
var config = require('../config/db.js');
var mongoose = require('mongoose');
// ensure the NODE_ENV is set to 'test'
// this is helpful when you would like to change behavior when testing
process.env.NODE_ENV = 'test';
beforeEach(function (done) {
function clearDB() {
for (var i in mongoose.connection.collections) {
mongoose.connection.collections[i].remove(function() {});
}
return done();
}
if (mongoose.connection.readyState === 0) {
mongoose.connect(config.url, function (err) {
if (err) {
throw err;
}
return clearDB();
});
} else {
return clearDB();
}
});
afterEach(function (done) {
mongoose.disconnect();
return done();
});
test/testUserModel.js
'use strict';
// import the moongoose helper utilities
var utils = require('../test/utils');
var should = require('should');
// import our User mongoose model
var User = require('../app/models/user.js');
var testUser1 = new Object({
profilePic: "testPic",
email: "testEmail",
first_name: "fname",
last_name: "lname",
description: "description",
personality: "personality",
phone_number: "phoneNum",
password: "password",
courses: {
course_name: "courseTest"
},
role: "testRole" //student/admin/teacher
});
var testUser2 = new Object({
profilePic: "testPic2",
email: "testEmail2",
first_name: "fname2",
last_name: "lname2",
description: "description2",
personality: "personality2",
phone_number: "phoneNum2",
password: "password2",
courses: {
course_name: "courseTest2"
},
role: "testRole2" //student/admin/teacher
});
describe('Users: models', function() {
describe('#register()', function() {
it('should register a new User', function (done) {
User.register(testUser1, function (err, createdUser) {
should.not.exist(err);
createdUser.email.should.equal("testEmail");
createdUser.first_name.should.equal("fname");
createdUser.phone_number.should.equal("phoneNum");
createdUser.password.should.equal("password");
createdUser.role.should.equal("testRole");
done();
});
});
});
describe('#getAllUsers', function() {
it('should return all users in DB', function (done) {
console.log("about to add stuff");
User.register(testUser1, function (err, createdUser) {
console.log(createdUser);
});
User.register(testUser2, function (err, createdUser) {
console.log(createdUser);
});
User.getAllUsers(function (err, createdUsers) {
should.not.exist(err);
createdUsers.should.be.instanceof(Array).and.have.lengthOf(2);
done();
});
});
});
});
我已经测试用户的注册功能,它在测试#register()在#getAllUsers工程确定, 然而()我在两个用户对象testUser1和testUSer2上进行注册。但是我的User.getAllUsers函数没有从数据库中返回任何东西。在部署这两个函数工作正常,你可以添加大量的用户,他们显示在GUI中的列表。看起来在User.register函数调用后,数据库得到了清除,所以当它到达User.getAllUsers时,什么都没有。有任何想法吗?
答
与您的代码:
describe('#getAllUsers', function() {
it('should return all users in DB', function (done) {
console.log("about to add stuff");
User.register(testUser1, function (err, createdUser) {
console.log(createdUser); // register was successfull
});
User.register(testUser2, function (err, createdUser) {
console.log(createdUser); //register was successfull
});
// Here, it should set in callback, when register function was successfull
User.getAllUsers(function (err, createdUsers) {
should.not.exist(err);
createdUsers.should.be.instanceof(Array).and.have.lengthOf(2);
done();
});
});
你可以看到寄存器中的例子是全成如下:
it('should return all users in DB', function (done) {
console.log("about to add stuff");
User.register(testUser1, function (err, createdUser) {
console.log(createdUser);
// here, it should set in callback, when register is successfull
User.getAllUsers(function (err, createdUsers2) {
should.not.exist(err);
createdUsers2.should.be.instanceof(Array).and.have.lengthOf(1);
done();
});
});
});