Javascript Jasmine expect(结果代码).toBe(200或409)
对于一些测试场景,我遇到了针对多个值进行测试的需要,这些值都是可以的 我想做的事情如下:Javascript Jasmine expect(结果代码).toBe(200或409),javascript,unit-testing,jasmine,Javascript,Unit Testing,Jasmine,对于一些测试场景,我遇到了针对多个值进行测试的需要,这些值都是可以的 我想做的事情如下: expect(resultCode).toBeIn([200,409]); 当resultCode为200或409时,此规范应通过。可能吗 已添加 感谢peter和dolarzo指导我创建matchers。我与addMatchers()有问题。因此,最后我在jasmine.js中添加了以下内容: jasmine.Matchers.prototype.toBeIn = function (expected)
expect(resultCode).toBeIn([200,409]);
当resultCode
为200
或409
时,此规范应通过。可能吗
已添加
感谢peter和dolarzo指导我创建matchers。我与addMatchers()有问题。因此,最后我在jasmine.js中添加了以下内容:
jasmine.Matchers.prototype.toBeIn = function (expected) {
for (var i = 0; i < expected.length; i++)
if (this.actual === expected[i])
return true;
return false;
};
jasmine.Matchers.prototype.toBeIn=函数(预期){
对于(变量i=0;i
这给了我一个有效的解决方案。我现在可以根据需要做托宾。(Jasmine 1.3.1)为了制作这样的作品:
expect(3).toBeIn([6,5,3,2]);
Jasmine有一个叫做matchers的功能:
这是一个关于如何声明它们的示例。我在最后声明了您正在寻找的方法:
describe('Calculator', function(){
var obj;
beforeEach(function(){
//initialize object
obj = new Object();
jasmine.addMatchers({
toBeFive: function () {
return {
compare: function (actual, expected) {
return {
pass: actual === 5,
message: actual + ' is not exactly 5'
}
}
};
},
toBeBetween: function (lower,higher) {
return {
compare: function (actual, lower,higher) {
return {
pass: ( actual>= lower && actual <= higher),
message: actual + ' is not between ' + lower + ' and ' + higher
}
}
};
},
toBeIn: function(expected) {
return {
compare: function (actual, expected) {
return {
pass: expected.some(function(item){ return item === actual; }),
message: actual + ' is not in ' + expected
}
}
};
}
});
});
重要茉莉花2.0。我必须使用jasmine.addMatchers({和jasmine specrunner.html),但是当我用Karma配置它时,我必须用this替换jasmine,就像this.addMatchers一样({因为Karma使用的是早期版本的Jasmine。试试看。Matchers是…谢谢你的指针。我尝试了你的建议,使用Jasmine.addMatcher和this.addMatcher…两者都不起作用。我必须找出原因,我认为另一个问题最好,因为它涉及到requirejs/typescript。同时,我我在jasmine.js中直接在原型上添加了一个新的匹配器,这很有效。我用我使用的结果更新了问题。
toBeIn: function(expected) {
return {
compare: function (actual, expected) {
return {
pass: expected.some(function(item){ return item === actual; }),
message: actual + ' is not in ' + expected
}
}
};
}