jasmine 测试用例不 return from mongo 保存
jasmine test case does not return from mongo save
我正在使用 https://github.com/mhevery/jasmine-node 来测试我的 nodejs 服务器路由。
我的猫鼬模型有一个工作前功能如下
userSchema.pre('save', function(next) {
var user = this;
if (!user.isModified('password')) return next();
bcrypt.genSalt(10, function(err, salt) {
if (err) return next(err);
logger.info('Hashing password!!!');
bcrypt.hash(user.password, salt, null, function(err, hash) {
if (err) return next(err);
user.password = hash;
next();
});
});
});
现在我需要在 jasmine 中编写一个测试用例,创建一个 userSchema 对象并将其保存到 mongodb 以便在我的测试用例中进一步使用它。
var User = require("../../../../models/User");
it("{postLogin - invalid}", function(done) {
var user = new User({email: "test@test.com", password: "a"});
user.save(function(err) {
if (err){
console.log(err);
return next(err);
}
console.log('user saved to db: ' +user.email);
request.post(invalidLoginParams, function(error, response, body) {
expect(response.statusCode).toBe(400);
done();
});
});
},15000);
我 运行 使用 jasmine-node 的上述测试用例,我得到日志 'Hashing password!!!' 这意味着它正在调用此函数。
但在这之后它再也没有 returns 到我的测试用例。它应该打印日志 'user saved to db: '
但它永远不会从 .pre('save') 函数返回。
知道我在哪里和错过了什么。无法在 google 上找到任何答案。希望能在这里得到!
谢谢
请确保正确调用回调"done()"。
我正在使用 https://github.com/mhevery/jasmine-node 来测试我的 nodejs 服务器路由。 我的猫鼬模型有一个工作前功能如下
userSchema.pre('save', function(next) {
var user = this;
if (!user.isModified('password')) return next();
bcrypt.genSalt(10, function(err, salt) {
if (err) return next(err);
logger.info('Hashing password!!!');
bcrypt.hash(user.password, salt, null, function(err, hash) {
if (err) return next(err);
user.password = hash;
next();
});
});
});
现在我需要在 jasmine 中编写一个测试用例,创建一个 userSchema 对象并将其保存到 mongodb 以便在我的测试用例中进一步使用它。
var User = require("../../../../models/User");
it("{postLogin - invalid}", function(done) {
var user = new User({email: "test@test.com", password: "a"});
user.save(function(err) {
if (err){
console.log(err);
return next(err);
}
console.log('user saved to db: ' +user.email);
request.post(invalidLoginParams, function(error, response, body) {
expect(response.statusCode).toBe(400);
done();
});
});
},15000);
我 运行 使用 jasmine-node 的上述测试用例,我得到日志 'Hashing password!!!' 这意味着它正在调用此函数。 但在这之后它再也没有 returns 到我的测试用例。它应该打印日志 'user saved to db: ' 但它永远不会从 .pre('save') 函数返回。 知道我在哪里和错过了什么。无法在 google 上找到任何答案。希望能在这里得到! 谢谢
请确保正确调用回调"done()"。