尝试在 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 :(
最佳答案
对于 jasmine 2.0,自定义匹配器的语法已更改。更新文档位于此处:http://jasmine.github.io/2.0/custom_matcher.html
关于javascript - 自定义匹配器使 Jasmine 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20855087/