测试框架Mocha

2017-05-04  本文已影响232人  07120665a058

可测试代码的几个原则:

测试

mocha -u tdd test.js

Mocha和Jasmine对比

两者功能覆盖范围粗略可以表示为:

Jasmine(2.x) === Mocha + Chai + Sinon - mockserver

Mocha

describe('save()', function() {
      it('should save without error', function(done) {
         ...
       done();
     });
});
//描述应该能连成一句通顺的话
//在对数据库操作的时候,可以加上done()回调函数,解决异步问题
//一个it只能调用一次done,

断言库

foo.should.be()
bar.should.have()
expect(foo).to.be()
expect(foo).to.eql()
assert: assert.equal(something, someting)
should: something.should.equal(something)
expect: expect(something).to.equal(someting)

demo地址

参考文章推荐:
How to correctly unit test Express server
使用Mocha和Chai来测试Node.js应用
测试框架 Mocha 实例教程
初识 mocha in NodeJS
测试利器mocha
HTTP 服务器测试库 supertest
Mocha官网

上一篇 下一篇

猜你喜欢

热点阅读