【Jasmine】行为驱动测试框架 -- 扩展教程
focus spec
只测试指定测试套件, 同一层级中出现 it
, fit
两个测试 spec
, 将忽略 it
, 同理,同一层级出现 describe
和fdescribe
,将会忽略desribe
案例
describe("Focused specs", function() { fit("is focused and will run", function() { expect(true).toBeTruthy(); }); // 忽略该测试 spec it('is not focused and will not run', function(){ expect(true).toBeFalsy(); }); fdescribe('focused describe', function(){ it('will run', function(){ expect(true).toBeTruthy(); }); it('will also run', function(){ expect(true).toBeTruthy(); }); }); fdescribe('another focused describe', function(){ // 忽略该测试 fit("is focused and will run", function() { expect(true).toBeTruthy(); }); it('is not focused and will not run', function(){ expect(true).toBeFalsy(); }); }); // 忽略该测试 describe('ignore describe', function () { fit("is focused and will run", function() { expect(true).toBeTruthy(); }); }) });
custom equality
自定义比较函数, 覆盖 toEqual
比较函数. 在beforeEach
声明周期使用 jasmine.addCustomEqualityTester
, 返回true
或 false
, 如果返回 undefined
或 null
. 则表示使用默认的toEqual
进行比较
describe('test suite', function () { beforeEach(function() { jasmine.addCustomEqualityTester(function (actual, expected) { var actualType = typeof actual if (actualType === 'string' && actualType === typeof expected) { return actual[0] === expected[0] } }); }); it('should be custom equal', function () { expect('aa').toEqual('ab'); }); });
custom matchers
自定义匹配规则, 在beforeEach
声明周期使用 jasmine.addMatchers
加入自定义匹配规则. 比 custom equality
更完善, 可以自定义错误信息.
beforeEach(function () { jasmine.addMatchers({ // TO BE LIKE toBeLike: function (util, customEqualityTesters) { return { // 匹配规则 // 参数 actual 实际传入值 // 参数 expected 期待值 compare: function (actual, expected) { var pass = false; var message = ''; var actualType = typeof actual; var expectedType = typeof expected; if (actual === expected) { pass = true; } else if (actual === null) { pass = expected === null || expected === undefined; } else if (actualType === expectedType) { pass = true; if (actualType === 'object') { for (name in actual) { if (actual[name] === null || actual[name] === undefined) { pass = expected === null || expected === undefined; } else if (typeof actual[name] !== typeof expected[name]) { pass = false; } if (!pass) { break; } } } } if (pass) { message = 'Expected ' + actual + ' to be like' } else { message = 'Expected ' + actual + ' not to be like' } return { pass: pass, message: message }; } // end compare }; } // end toBeLike function }); });
使用方式
describe('to be like suite', () => { function noop () {} function noop2 () {} it('assert to be like', function () { expect('f').toBeLike('a'); expect(null).toBeLike(undefined); expect({ msg: 'first msg', cb: noop }).toBeLike({ msg: 'second msg', cb: noop2 }); }); it('assert no to be like', function () { expect(1).not.toBeLike('1'); expect(noop).not.toBeLike(); }); });
相关文章推荐
- Jasmine基础教程
相关推荐
runner 2020-09-01
JASMINE花酱 2020-04-16
酒囊饭袋 2020-03-11
runner 2019-12-12
灰帽jasmine 2016-11-15
Blovesea 2016-07-06
JASMINE花酱 2019-06-27
jinhailion 2014-11-16
wangmiaoyan 2014-09-04
thug 2019-06-26
83437713 2014-05-20
itclkang 2014-05-15
runner 2014-01-17
jinhailion 2019-06-25
灰帽jasmine 2019-06-21
luochaotj 2013-03-28
dllyj 2012-05-11
JASMINE花酱 2018-12-26
swywkci 2019-05-08