是否可以在Jasmine.Js中检查toEqual中的多个类型?

时间:2013-02-22 17:31:17

标签: javascript jasmine

我是Jasmine的新手,并遇到了我希望String或null的情况。我尝试做一个或在toEqual之内,但我看到一些奇怪的结果让我相信我会以错误的方式进行。这种情况的最佳方法是什么?

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

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. Expected null to equal <jasmine.any(function String() { [native code] })>.
  2. Expected null to equal <jasmine.any(function String() { [native code] })>.
  3. 我意识到还有一个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();
        });
    });
    

2 个答案:

答案 0 :(得分:12)

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

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 :(得分:1)

如果您确实希望这些案例能够在同一个测试中工作,那么您可以编写自己的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;
}