javascript - 自定义匹配器使 Jasmine 挂起

标签 javascript unit-testing testing jasmine

尝试在 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/

相关文章:

javascript - 在 : memory allocation versus large object manipulation 中

javascript - 与正方形碰撞后如何保持圆周速度?

javascript - 我如何使用 Sinon stub $(window).width() ?

java - 我如何使用 spring Autowiring 使用内存中的 derby 数据库来使用一堆 DAO?

selenium webdriver 有没有其他方法可以在不使用 "findElements()"方法的情况下找出网页中存在的网络元素的数量?

Iphone Ipad QA 测试流程

javascript - 无法将 Bootstrap 或 React-Bootstrap 样式应用于使用 create-react-app 创建的应用程序

javascript - Codeigniter 上的 AutoSuggest (Ajax) - 404 错误

python-3.x - 如何在 Python 单元测试中模拟 readlines()

unit-testing - 避免单元测试中的大引用数据