CNode

mocha + chaijs + bluebird + mongoose测试会报错,但显示通过

分享
Ii5ting发布于11 年前最后回复11 年前5 回复3824 浏览0 收藏

mocha + chaijs + bluebird + mongoose测试问题

testProducts.saveAsync().then(function(prod){
  return Product.get_all_unchecked(function(err, products){
    Promise.resolve(products);
  });
}).then(function(products){
  assert.lengthOf(products, 13, 'products`s value has a length of 3');
  done()
}).catch(function(err) {
  done(err);
}); 

说明:

当捕获到异常的时候必须将异常传给done函数,否则测试会报错,但显示通过

  done(err);

只能说明自己对mocha api理解的不到位

官方的一个非常好的例子,把done的各种用法都说明的非常清楚了

describe('Connection', function(){
  var db = new Connection
    , tobi = new User('tobi')
    , loki = new User('loki')
    , jane = new User('jane');

  beforeEach(function(done){
    db.clear(function(err){
      if (err) return done(err);
      db.save([tobi, loki, jane], done);
    });
  })

  describe('#find()', function(){
    it('respond with matching records', function(done){
      db.find({ type: 'User' }, function(err, res){
        if (err) return done(err);
        res.should.have.length(3);
        done();
      })
    })
  })
})
查看回复

回复 (5)

D
dayuoba#1·11 年前

为什么不把error加入断言呢?

I
i5ting#2·11 年前
D
dayuoba#3·11 年前
引用 i5ting@dayuoba 请指教

@i5ting

var fs = require('fs');
var should = require('should');
describe('test', function() {
    it('should has no err,and has data', function(done) {
        fs.readFile('a.txt', function(err, data) {
            this.timeout = 1000;
            should.not.exist(err);
            should.exist(result);
            done();
        });
    });
});
I
i5ting#4·11 年前
引用 dayuoba@i5ting

@dayuoba 谢谢~~

D
dayuoba#5·11 年前
引用 i5ting@dayuoba 谢谢

@i5ting you are welcome:)

参与回复
登录后即可参与回复。登录