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

Jasmine expect logic (expect A OR B)(Jasmine 期望逻辑(期望 A OR B))
本文介绍了Jasmine 期望逻辑(期望 A OR B)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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

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

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

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

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;
    }
  }
}

所以看起来您确实需要编写自己的匹配器(从 beforeit 块中获取正确的 this).例如:

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

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;
   }
});

这篇关于Jasmine 期望逻辑(期望 A OR B)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

jasmine.createSpyObj with properties(jasmine.createSpyObj 与属性)
What#39;s a good way to reuse test code using Jasmine?(使用 Jasmine 重用测试代码的好方法是什么?)
Does Jasmine#39;s toThrow matcher require the argument to be wrapped in an anonymous function?(Jasmine 的 toThrow 匹配器是否需要将参数包装在匿名函数中?)
How to mock window.location.href with Jest + Vuejs?(如何用 Jest + Vuejs 模拟 window.location.href?)
Jasmine - Spying on a method call within a constructor(Jasmine - 在构造函数中监视方法调用)
Getting requirejs to work with Jasmine(让 requirejs 与 Jasmine 一起工作)