我不断收到错误在save()方法,当我运行测试。
var User = require('../../models/user')
, should = require('should');
describe('User', function(){
describe('#save()', function(){
it('should save without error', function(done){
var user = new User({
username : 'User1'
, email : 'user1@example.com'
, password : 'foo'
});
user.save(function(err, user){
if (err) throw err;
it('should have a username', function(done){
user.should.have.property('username', 'User1');
done();
});
});
})
})
})
这里是错误:
$ mocha test/unit/user.js
․
✖ 1 of 1 test failed:
1) User #save() should save without error:
Error: timeout of 2000ms exceeded
at Object.<anonymous> (/usr/local/lib/node_modules/mocha/lib/runnable.js:1
61:14)
at Timer.list.ontimeout (timers.js:101:19)
您可以嵌套描述,但没有它测试。 每个测试是为了是独立的,所以当你正在寻找通过你的结果,你能看到它是失败 - 在保存或没有用户名属性。 例如,在你的代码上面有没有办法,因为没有做过失败的“应该保存而不会出现错误”测试()。 这也是上面的代码已超时的原因:摩卡找不到()完成了“应该保存没有错误”的测试。
能够巢描述是非常强大的,但。 在每个形容你可以有一个之前,beforeEach,后afterEach声明。 有了这些,你可以实现你在上面尝试嵌套。 如果您想对这些报表一并阅读。请参见摩卡文档的更多信息。
我会写你想达到什么样的方法如下:
var User = require('../../models/user')
, should = require('should')
// this allows you to access fakeUser from each test
, fakeUser;
describe('User', function(){
beforeEach(function(done){
fakeUser = {
username : 'User1'
, email : 'user1@example.com'
, password : 'foo'
}
// this cleans out your database before each test
User.remove(done);
});
describe('#save()', function(){
var user;
// you can use beforeEach in each nested describe
beforeEach(function(done){
user = new User(fakeUser);
done();
}
// you are testing for errors when checking for properties
// no need for explicit save test
it('should have username property', function(done){
user.save(function(err, user){
// dont do this: if (err) throw err; - use a test
should.not.exist(err);
user.should.have.property('username', 'User1');
done();
});
});
// now try a negative test
it('should not save if username is not present', function(done){
user.username = '';
user.save(function(err, user){
should.exist(err);
should.not.exist(user.username);
done();
});
});
});
});