我正在编写一系列测试来演示“this”在不同上下文中的值(value)。
这是我当前的测试套件:
describe("this keyword", function(){
it('returns the global context of this', function(){
expect(globalThis()).toEqual(window);
});
it('returns the method context of this', function(){
expect(methodThis.showThis()).toEqual({ showThis : Function });
});
});
即使它是确切的值,我的第二个测试也不会通过此代码:
var methodThis = {
showThis: function(){
return this;
}
};
这个函数所做的就是返回对象内部 this 的上下文。
即使将正确的 toEqual 值传递给 toEqual,为什么此测试仍失败?
最佳答案
您可以使用jasmine.any用于比较函数类型:
describe("this keyword", function(){
it('returns the method context of this', function(){
expect(methodThis.showThis()).toEqual({ showThis : jasmine.any(Function) });
});
});
关于javascript - Jasmine 测试 toEqual 是正确的值,但测试仍然失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24125788/