jasmine-自定义Matchers

当然,用户可以自定义Matchers。在beforeEach()或it()函数里调用Jasmine.addMatchers(),其中可以传递一个参数expected作为测试值,而实际值则保存在this.actual中,代码如下:

describe("This is a suite", function() {

  beforeEach(function(){

  jasmine.addMatchers({

    toBeSomeThing: function(){  //定义断言的名字

      return {

        compare: function (actual, expected) {  //compare是必须的

          var foo = actual;

          return {

               pass: foo === expected || 'something' ,

               message: "error message here"  //断言为false时的信息

          }  //要返回一个带pass属性的对象,pass就是需要返回的布尔值


       //negativeCompare: function(actual, expected){ ... }  //自定义not.的用法

        }

      };

    }

  });

});

});

你可能感兴趣的:(jasmine-自定义Matchers)