【问题标题】:custom matcher makes jasmine to hang自定义匹配器使茉莉花挂起
【发布时间】:2013-12-31 09:33:48
【问题描述】:

尝试在 jasmine 测试工具中调用此自定义匹配器,但出现此错误:

TypeError: matcherCompare is undefined
var result = matcherCompare.apply(null, args);
jasmine.js (line 1192)

我的匹配器:

/*
 * Extends jasmine expectations by defining new matchers
 */
beforeEach(function () {
   jasmine.addMatchers({
      toEqualArray: function(){
         var s = typeof this.actual,
             result = false; 
         if (s === 'object'){
            if (this.actual){
               if (Object.prototype.toString.call(this.actual) === Object.prototype.toString.call([])) { //'[object Array]'
                  result = true;
               }
            }
         }
         this.message = function(){
            if (result){
               return "Is Array";
            }
            return "Is not an Array";
         };
         return result;
      }
   });
});

toEqualArray里面的代码核心已经作为一个简单的js函数测试过了,没问题。如您所见,我的匹配器没有参数。我使用 jasmine 2.0 独立版本进行测试,而我的匹配器位于外部 js 文件中,就像在 jasmine 独立版本的示例中一样。我什至在我的规范中移动了我的匹配器,用this 替换了jasmine,但没有结果! 我做错了什么?

当我在我的规范中输入这个特定命令时,Jasmine 挂起:

expect(o.get('any')).toEqualArray();

o 是我的对象,它返回(我测试过,没问题)一个数组!

我现在必须调试 jasmine :(

【问题讨论】:

标签: javascript unit-testing testing jasmine


【解决方案1】:

对于 jasmine 2.0,自定义匹配器的语法已更改。更新的文档在这里:http://jasmine.github.io/2.0/custom_matcher.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    相关资源
    最近更新 更多