我是 Jasmine 的新手和一般测试的新手。我的代码块检查我的库是否已使用 new 运算符实例化:
//if 'this' isn't an instance of mylib...
if (!(this instanceof mylib)) {
//return a new instance
return new mylib();
}
我如何使用 Jasmine 对此进行测试?
最佳答案
Jasmine >=3.5.0
Jasmine 提供了 toBeInstanceOf
匹配器。
it("matches any value", () => {
expect(3).toBeInstanceOf(Number);
});
Jasmine >2.3.0
检查某物是否是instanceof [Object]
Jasmine 提供了jasmine.any
:
it("matches any value", function() {
expect({}).toEqual(jasmine.any(Object));
expect(12).toEqual(jasmine.any(Number));
});
关于javascript - 使用 Jasmine 测试 instanceof,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24332917/