我为 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
所以主要问题实际上是:当我将自定义匹配器添加到 Jasmine 时,它们存储在哪里?我可以检索它们的规格还是隐藏在封闭中?
编辑
似乎addMatchers
正在调用env.addMatchers
,这将匹配器推到runnableResources[currentRunnable().id].customMatchers
. RunnableResources 是一个闭包...