【发布时间】:2015-06-18 18:09:44
【问题描述】:
出于测试目的,我需要撤消加载 mongoose 的文档中的更改。
即。我尝试将某些内容设置为错误的类型,当然,失败了。然后我尝试别的东西,但我在第一个错误上失败了。示例:
// here I load the user
before(function(done) {
user = loadUserSomehow();
done();
})
it ('should validate email', function(done) {
// now I try one field wrong
user.email = 'This is not a valid email';
user.save(function(err) {
err.message.should.equal.'Invalid email'; // to simplify
// I would get 'invalid email' error here.
});
});
it ('should validate type', function(done) {
// now I try another field wrong
user.type = 'This type does not exist';
user.save(function(err) {
err.message.should.equal.'Invalid user type'; // THIS fails
// instead of invalid type, I still get an invalid email message
});
});
为了避免在每次测试之前重新加载文档,我如何“撤消”对 mongoose 文档的更改?
【问题讨论】:
-
附带说明,您没有在测试中调用
done() -
您找到了更好的解决方案,如果您能回答自己的问题,这样其他人可以从您的研究中受益,那就太好了。谢谢
-
我想我使用了一些模拟数据库设置。那是很久以前的事了,所以我真的不记得了。
标签: node.js testing mongoose mocha.js