如果满足两个期望中的一个,我需要设置测试成功:
expect(mySpy.mostRecentCall.args[0]).toEqual(jasmine.any(Number));
expect(mySpy.mostRecentCall.args[0]).toEqual(false);我希望它看起来像这样:
expect(mySpy.mostRecentCall.args[0]).toEqual(jasmine.any(Number)).or.toEqual(false);有没有我在文档中遗漏了什么,或者我必须编写自己的匹配器?
发布于 2012-11-23 22:09:19
注意:此解决方案包含Jasmine v2.0之前版本的语法。有关自定义匹配器的更多信息,请参阅:https://jasmine.github.io/2.0/custom_matcher.html
Matchers.js只能使用单个'result modifier‘- not
核心/规范.js:
jasmine.Spec.prototype.expect = function(actual) {
var positive = new (this.getMatchersClass_())(this.env, actual, this);
positive.not = new (this.getMatchersClass_())(this.env, actual, this, true);
return positive;core/Matchers.js:
jasmine.Matchers = function(env, actual, spec, opt_isNot) {
...
this.isNot = opt_isNot || false;
}
...
jasmine.Matchers.matcherFn_ = function(matcherName, matcherFunction) {
return function() {
...
if (this.isNot) {
result = !result;
}
}
}因此,看起来您确实需要编写自己匹配器(从before或it块中获取正确的this)。例如:
this.addMatchers({
toBeAnyOf: function(expecteds) {
var result = false;
for (var i = 0, l = expecteds.length; i < l; i++) {
if (this.actual === expecteds[i]) {
result = true;
break;
}
}
return result;
}
});发布于 2017-02-19 00:42:35
将多个可比较的字符串添加到数组中,然后进行比较。颠倒比较的顺序。
expect(["New", "In Progress"]).toContain(Status);发布于 2016-05-13 08:48:50
这是一个古老的问题,但如果有人还在寻找,我有另一个答案。
构建逻辑OR表达式并期待它会怎样呢?如下所示:
var argIsANumber = !isNaN(mySpy.mostRecentCall.args[0]);
var argIsBooleanFalse = (mySpy.mostRecentCall.args[0] === false);
expect( argIsANumber || argIsBooleanFalse ).toBe(true);这样,您就可以显式地测试/预期OR条件,并且只需要使用Jasmine来测试布尔匹配/不匹配。将在Jasmine 1或Jasmine 2中工作:)
https://stackoverflow.com/questions/13530365
复制相似问题