【发布时间】:2015-07-28 18:03:12
【问题描述】:
我们添加了一个toHaveClass 自定义茉莉花匹配器,为了使其工作,我们必须将它添加到beforeEach()(在this topic 的帮助下)。
并且,为了遵循 DRY 原则并避免在需要 toHaveClass 的规范中在每个 beforeEach() 中重复匹配器定义,我们在 onPrepare() 中添加了一个 beforeEach() 块:
onPrepare: function () {
var jasmineReporters = require("jasmine-reporters");
require("jasmine-expect");
// ...
// custom matchers
beforeEach(function() {
jasmine.addMatchers({
toHaveClass: function() {
return {
compare: function(actual, expected) {
return {
pass: actual.getAttribute("class").then(function(classes) {
return classes.split(" ").indexOf(expected) !== -1;
})
};
}
};
}
});
});
},
它确实有效,但是每次我在量角器配置中看到beforeEach() 块时,我都会有一种微压抑和强烈的感觉,这不是定义匹配器的好地方。
问题:
有没有更好的方法或地方来定义自定义匹配器?
【问题讨论】:
标签: javascript angularjs testing jasmine protractor