var assert = require("assert") describe('Array', function(){ describe('#indexOf()', function(){ it('should return -1 when the value is not present', function(){ assert.equal(-1, [1,2,3].indexOf(5)); assert.equal(-1, [1,2,3].indexOf(0)); }) }) })
官网的说法是:Testing asynchronous code with Mocha could not be simpler!(用Mocha做异步测试已经不能再简单!) 官方的例子是:
1 2 3 4 5 6 7 8 9 10 11
describe('User', function(){ describe('#save()', function(){ it('should save without error', function(done){ var user = new User('Luna'); user.save(function(err){ if (err) throw err; done(); }); }) }) })
describe('User', function(){ describe('#save()', function(){ it('should save without error', function(done){ var user = new User('Luna'); user.save(done); }) }) })
describe('Connection', function(){ //连接数据库并本地创建3个User var db = new Connection , tobi = new User('tobi') , loki = new User('loki') , jane = new User('jane');
describe('hooks', function() { before(function() { // runs before all tests in this block }) after(function(){ // runs after all tests in this block }) beforeEach(function(){ // runs before each test in this block }) afterEach(function(){ // runs after each test in this block }) // test cases })