【问题标题】:Is it possible to check against multiple types within a toEqual in Jasmine.Js?是否可以在 Jasmine.Js 中的 toEqual 中检查多种类型?
【发布时间】:2013-02-08 09:54:20
【问题描述】:

我对 Jasmine 很陌生,遇到了我期望 String 或 null 的情况。我试图在 toEqual 中做 or ,但我看到了一些奇怪的结果,这让我相信我正在以错误的方式解决这个问题。处理这种情况的最佳方法是什么?

也许我只是在测试错误。我是否应该放弃使用一个测试来测试两种情况的想法?

describe("Jasmine", function () {

    //1
    it("should be able to handle any(String) || null within toEqual for string", function () {
        expect("aString").toEqual(jasmine.any(String) || null);
    });

    //2
    it("should be able to handle any(String) || null within toEqual for null", function () {
        expect(null).toEqual(jasmine.any(String) || null);
    });

    //3
    it("should be able to handle null || any(String) within toEqual for string", function () {
        expect("aString").toEqual(null || jasmine.any(String));
    });

    //4
    it("should be able to handle null || any(String) within toEqual for null", function () {
        expect(null).toEqual(null || jasmine.any(String));
    });
});
  1. 通过
  2. Expected null to equal <jasmine.any(function String() { [native code] })>.
  3. 通过
  4. Expected null to equal <jasmine.any(function String() { [native code] })>.

我意识到还有一个 toBeNull() 这可能是结果如此不稳定的原因,但是没有“或”链接我不知道如何合并它。

(运行 Jasmine 1.3.1 修订版 1354556913)

解决了!如果有人感兴趣,请参阅下面的完整解决方案

describe("Jasmine", function () {

    beforeEach(function () {
        this.addMatchers({

            toBeStringOrNull: function () {
                var actual = this.actual;

                this.message = function () {
                    return "Expected " + actual + " to be either string or null";
                };

                return typeof actual === 'string' || actual instanceof String || actual === null;
            }

        });
    });

    //1
    it("should be able to handle any(String) || null within toEqual for string", function () {
        expect("aString").toBeStringOrNull();
    });

    //2
    it("should be able to handle any(String) || null within toEqual for null", function () {
        expect(null).toBeStringOrNull();
    });

    //3
    it("should be able to handle null || any(String) within toEqual for string", function () {
        expect("aString").toBeStringOrNull();
    });

    //4
    it("should be able to handle null || any(String) within toEqual for null", function () {
        expect(null).toBeStringOrNull();
    });
});

【问题讨论】:

  • 您是否希望单个输入同时为您提供stringnull?我不明白为什么单个expect 需要涵盖这两种情况。
  • @Mathletics 我也希望如此,这就是我要撞墙的原因。我试图创建一个测试,以便在集成测试期间我可以在这两种情况下重用它。我想我意识到这不是解决问题的标准方法。
  • 您能否展示一个方法示例,该方法从单个输入中可能同时返回两者?
  • @Mathletics 我正在从服务器接收不同项目的数据,这些项目有时具有填充字符串,否则为空。这是两种可能的结果,所以我想对此进行测试。我认为在集成测试中进行一些重用会很好。

标签: javascript jasmine


【解决方案1】:

编写您自己的自定义匹配器:

toBeStringOrNull: function() {
  var actual = this.actual;

  this.message = function () {
    return "Expected " + actual + " to be either string or null";
  }

  return typeof actual === 'string' || actual instanceof String || actual === null;
}

【讨论】:

    【解决方案2】:

    如果您确实希望这些案例在同一个测试中工作,您可以编写自己的matcher。类似的东西

    toBeStringOrNull: function() {
      var actual = this.actual;
    
      this.message = function () {
        return "Expected " + actual + " to be either string or null";
      }
    
      return jasmine.any(String) || actual === null;
    }
    

    【讨论】:

    • 壮观,谢谢。我想我会尽量避免我正在尝试的事情,但很高兴知道我可以在 Jasmine 中相当轻松地完成这样的事情。
    • 这不起作用。返回 jasmine.any(String) 表示上述匹配器始终返回 true。有关如何在另一个匹配器中使用一个匹配器的更多信息,请参阅stackoverflow.com/questions/8947787/…
    • @tukushan 你完全正确。感谢指正和指点。
    猜你喜欢
    • 2013-07-14
    • 2019-02-19
    • 1970-01-01
    • 2021-11-16
    • 2017-02-01
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多