I am testing my model with mocha. I have the following files:
test/utils.js
'use strict';
// Modified from http://ift.tt/1BdJifx
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();
});
});
});
});
I have tested the register function of User and it works ok in test #register(), However in #getAllUsers() I do a register on two user objects, testUser1 and testUSer2. But my User.getAllUsers function is returning nothing from the DB. In deployment the two functions work fine, you can add lots of users and they display in a list in GUI. It seems after the User.register function call the DB is getting cleared so when it gets to User.getAllUsers theres nothing there. Any ideas?
Aucun commentaire:
Enregistrer un commentaire