Pau*_*515 6 javascript unit-testing jasmine
对于某些测试场景,我遇到了针对多个值进行测试的需要,这些值都可以.
我想做的是如下:
expect(resultCode).toBeIn([200,409]);
Run Code Online (Sandbox Code Playgroud)
当resultCode或者是200或时,该规范应该通过409.那可能吗?
添加 感谢peter和dolarzo指导我创建匹配器.我有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;
};
Run Code Online (Sandbox Code Playgroud)
这给了我一个有效的解决方案.我现在可以根据需要做toBeIn.(Jasmine 1.3.1)
为了做这样的工作:
expect(3).toBeIn([6,5,3,2]);
Run Code Online (Sandbox Code Playgroud)
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
}
}
};
}
});
});
Run Code Online (Sandbox Code Playgroud)
这是你需要的匹配器:
toBeIn: function(expected) {
return {
compare: function (actual, expected) {
return {
pass: expected.some(function(item){ return item === actual; }),
message: actual + ' is not in ' + expected
}
}
};
}
Run Code Online (Sandbox Code Playgroud)
茉莉花2.0很重要.我不得不使用jasmine.addMatchers({茉莉花specrunner.html但是当我噶配置它,我不得不更换茉莉花与此类似this.addMatchers({因为噶使用茉莉花的早期版本.
| 归档时间: |
|
| 查看次数: |
1009 次 |
| 最近记录: |