【Jasmine】行为驱动测试框架 -- 扩展教程

wayneli

focus spec

只测试指定测试套件, 同一层级中出现 it, fit 两个测试 spec, 将忽略 it, 同理,同一层级出现 describefdescribe,将会忽略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, 返回truefalse, 如果返回 undefinednull. 则表示使用默认的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();
  });
});

相关文章推荐

阅读 1.3k

全栈工程师之路
一个前端工程师如何走向全面的全栈工程师, 首先得打好基础。

2017-2018年目标

1.4k 声望
824 粉丝
0 条评论
你知道吗?

2017-2018年目标

1.4k 声望
824 粉丝
宣传栏