Mocha
Mocha 是JavaScript 测试框架之一
使用环境 浏览器和 Node环境
1.基本测试脚本
通常,测试脚本 以同名的 .test.js 或者 .spec.js来命名
实例如下:
//本文件名 app.spec.js
var app = require('app.js');
var expect = require('chai').expect;
describe('test app.js ',function(){
it('test app 1 example',function(){
//apps 为app(1)的期望值
expect(app(1)).to.equal(apps)
})
})
测试脚本包含
- 一个或多个 describe (测试套件)
- 每个 describe 包含 一个或多个 it (测试用例)
2.断言库
断言:就是判断源码的实际执行结果与预期结果是否一致
expect(app(1)).to.equal(apps)
一些chai的使用例子
//should 用法
foo.should.be.a('string');
foo.should.equal('bar');
foo.should.have.lengthOf(3);
tea.should.have.property('flavors')
.with.lengthOf(3);
//expect用法
expect(foo).to.be.a('string');
expect(foo).to.equal('bar');
expect(foo).to.have.lengthOf(3);
expect(tea).to.have.property('flavors')
.with.lengthOf(3);
//assert
assert.typeOf(foo, 'string');
assert.equal(foo, 'bar');
assert.lengthOf(foo, 3)
assert.property(tea, 'flavors');
assert.lengthOf(tea.flavors, 3);