如何重写Jasmine 2.0自定义匹配器与Angular 1.5一起使用?

时间:2016-04-01 22:10:46

标签: javascript angularjs jasmine

AngularJS tutorial自定义匹配器无法在Jasmine starting from version 2.0中运行:

beforeEach(function(){
    this.addMatchers({
      toEqualData: function(expected) {
        return angular.equals(this.actual, expected);
      }
    });
  });

修改匹配器的尝试失败,错误:

TypeError: undefined is not an object (evaluating 'matcherCompare.apply')

我的匹配器定义:

beforeEach(function(){
    jasmine.addMatchers({
      toEqualData: function(util, customEqualityTesters, actual, expected) {
        return angular.equals(actual, expected);
      }
    });
  });

Jasmine 2.0 custom matcher docs

如何让它发挥作用?

1 个答案:

答案 0 :(得分:1)

beforeEach(function () {
    jasmine.addMatchers({
      toEqualData: function () {
        return {
          compare: function (actual, expected) {
            return {pass: angular.equals(actual, expected)};
          }
        };
      }
    });
  });