cho*_*ovy 15 unit-testing mocha.js mongoose node.js
我试图找出在所有测试运行后将函数删除数据库和关闭连接的位置.
这是我的嵌套测试:
//db.connection.db.dropDatabase();
//db.connection.close();
describe('User', function(){
beforeEach(function(done){
});
after(function(done){
});
describe('#save()', function(){
beforeEach(function(done){
});
it('should have username property', function(done){
user.save(function(err, user){
done();
});
});
// now try a negative test
it('should not save if username is not present', function(done){
user.save(function(err, user){
done();
});
});
});
describe('#find()', function(){
beforeEach(function(done){
user.save(function(err, user){
done();
});
});
it('should find user by email', function(done){
User.findOne({email: fakeUser.email}, function(err, user){
done();
});
});
it('should find user by username', function(done){
User.findOne({username: fakeUser.username}, function(err, user){
done();
});
});
});
});
Run Code Online (Sandbox Code Playgroud)
似乎没什么用.我得到错误:超过2000毫秒的超时
Jon*_*ski 23
你可以after()在第一个之前定义一个"root" 钩子describe()来处理清理:
after(function (done) {
db.connection.db.dropDatabase(function () {
db.connection.close(function () {
done();
});
});
});
describe('User', ...);
Run Code Online (Sandbox Code Playgroud)
虽然,你得到的错误可能来自3个不通知Mocha继续的异步挂钩.这些需要调用done()或跳过参数,以便将它们视为同步:
describe('User', function(){
beforeEach(function(done){ // arg = asynchronous
done();
});
after(function(done){
done()
});
describe('#save()', function(){
beforeEach(function(){ // no arg = synchronous
});
// ...
});
});
Run Code Online (Sandbox Code Playgroud)
来自文档:
通过向Mocha 添加回调(通常命名
done)it()将知道它应该等待完成.
| 归档时间: |
|
| 查看次数: |
8142 次 |
| 最近记录: |