javascript Jasmine 期望逻辑(期望 A OR B)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13530365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:04:18  来源:igfitidea点击:

Jasmine expect logic (expect A OR B)

javascriptunit-testinglogicjasmine

提问by naugtur

I need to set the test to succeed if one of the two expectations is met:

如果满足以下两个期望之一,我需要将测试设置为成功:

expect(mySpy.mostRecentCall.args[0]).toEqual(jasmine.any(Number));
expect(mySpy.mostRecentCall.args[0]).toEqual(false);

I expected it to look like this:

我希望它看起来像这样:

expect(mySpy.mostRecentCall.args[0]).toEqual(jasmine.any(Number)).or.toEqual(false);

Is there anything I missed in the docs or do I have to write my own matcher?

我在文档中遗漏了什么还是我必须编写自己的匹配器?

采纳答案by raina77ow

Note: This solution contains syntax for versions prior to Jasmine v2.0. For more information on custom matchers now, see: https://jasmine.github.io/2.0/custom_matcher.html

注意:此解决方案包含 Jasmine v2.0 之前版本的语法。有关自定义匹配器的更多信息,请参见:https: //jasmine.github.io/2.0/custom_matcher.html



Matchers.js works with a single 'result modifier' only - not:

Matchers.js 仅适用于单个“结果修饰符” - not

core/Spec.js:

核心/Spec.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:

核心/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;
    }
  }
}

So it looks like you indeed need to write your own matcher (from within a beforeor itbloc for correct this). For example:

所以看起来您确实需要编写自己的匹配器(从 abeforeitbloc 中编写正确的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;
   }
});

回答by Zs Felber

Add multiple comparable strings into an array and then compare. Reverse the order of comparison.

将多个可比较的字符串添加到数组中,然后进行比较。颠倒比较顺序。

expect(["New", "In Progress"]).toContain(Status);

回答by RoboBear

This is an old question, but in case anyone is still looking I have another answer.

这是一个老问题,但如果有人仍在寻找,我有另一个答案。

How about building the logical OR expression and just expecting that? Like this:

如何构建逻辑 OR 表达式并期待它?像这样:

var argIsANumber = !isNaN(mySpy.mostRecentCall.args[0]);
var argIsBooleanFalse = (mySpy.mostRecentCall.args[0] === false);

expect( argIsANumber || argIsBooleanFalse ).toBe(true);

This way, you can explicitly test/expect the OR condition, and you just need to use Jasmine to test for a Boolean match/mismatch. Will work in Jasmine 1 or Jasmine 2 :)

这样,您可以显式地测试/期望 OR 条件,并且您只需要使用 Jasmine 来测试布尔匹配/不匹配。将在 Jasmine 1 或 Jasmine 2 中工作:)

回答by SteveisStuck

You can take the comparison out of the expect statement to gain full use of comparison operators.

您可以从 expect 语句中取出比较,以充分利用比较运算符。

let expectResult = (typeof(await varA) == "number" || typeof(await varA) == "object" );
expect (expectResult).toBe(true);