Jasmine expect(resultCode).toBe(200 or 409)(Jasmine expect(resultCode).toBe(200 or 409))
问题描述
对于某些测试场景,我需要针对多个值进行测试,这些值都可以.
For some test scenarios I run into the need of testing against multiple values which are all OK.
我想做的事情如下:
expect(resultCode).toBeIn([200,409]);
当 resultCode
为 200
或 409
时,此规范应通过.这可能吗?
This spec should pass when resultCode
is either 200
or 409
. Is that possible?
添加感谢 peter 和 dolarzo 指导我创建匹配器.我遇到了 addMatchers() 的问题.所以,最后我在 jasmine.js 中添加了以下内容:
ADDED Thanks to peter and dolarzo for pointing me towards creating matchers. I had problems with addMatchers(). So, in the end I added the following to the 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;
};
这给了我一个可行的解决方案.我现在可以根据需要做 toBeIn.(茉莉花1.3.1)
This gave me a working solution. I can now do the toBeIn as needed. (Jasmine 1.3.1)
推荐答案
为了做这样的事情:
expect(3).toBeIn([6,5,3,2]);
Jasmine 有一个称为匹配器的功能:
Jasmine has a feature called matchers:
这是一个关于如何声明它们的示例.我在最后声明了您正在寻找的方法:
This is an example on how to declare them. I have declared at the very end the method you are looking for:
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
}
}
};
}
});
});
这是你需要的匹配器:
toBeIn: function(expected) {
return {
compare: function (actual, expected) {
return {
pass: expected.some(function(item){ return item === actual; }),
message: actual + ' is not in ' + expected
}
}
};
}
重要与 jasmine 2.0.我必须将 jasmine.addMatchers({ 与 jasmine specrunner.html 一起使用,但是当我使用 Karma 对其进行配置时,我必须将 jasmine 替换为 this 之类的this.addMatchers({ 因为 Karma 使用早期版本的 Jasmine.
Important with jasmine 2.0. I had to use jasmine.addMatchers({ with jasmine specrunner.html but when I configured it with Karma I had to replace jasmine with this like this.addMatchers({ because Karma use an earlier version of Jasmine.
这篇关于Jasmine expect(resultCode).toBe(200 or 409)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Jasmine expect(resultCode).toBe(200 or 409)
基础教程推荐
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 动态更新多个选择框 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 我什么时候应该在导入时使用方括号 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01