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);