如何测试自定义的Jasmine匹配器?

时间:2015-05-05 15:56:18

标签: jasmine jasmine2.0

我为Jasmine 2.x创建了一些自定义匹配器,我想为其创建规格。 大多数程序员只是在测试中应用它们来测试他们的匹配器,验证输出代码是否为正。

例如.toEqual'自定义'匹配器:

it('should test if two objects are equal', function () {
  expect({}).toEqual({}); // Passing
});

但是,我想测试匹配器的失败情况,我想测试匹配器失败时产生的消息,例如以这种方式。

// Jasmine 2.x
it('should test if two objects are equal', function () {
  expect(matcher.compare(1, 1)).toEqual({
    pass: true,
    message: 'Expected 1 not to equal 1' // Message is for when you use .not with the matcher 
  });
  expect(matcher.compare(1, 2)).toEqual({
    pass: false,
    message: 'Expected 1 to equal 2'
  });
});

代码示例中的变量matcher是我使用jasmine.addMatchers({ toEqual: matcher })添加的匹配器函数。

由于我不想对全球空间进行调查,因此我不知道如何在我的规范中提供匹配器。默认的jasmine匹配器位于jasmine.matchers中,并通过某个$j对象添加到their spec

所以主要的问题是:当我将它们添加到Jasmine时,我的自定义匹配器存储在哪里?我可以为规格检索它们还是隐藏在闭包中?

修改

似乎addMatchers正在调用env.addMatchers,这会将匹配器推送到runnableResources[currentRunnable().id].customMatchers。 RunnableResources是一个闭包......

0 个答案:

没有答案
相关问题