gpt4 book ai didi

javascript - Jasmine 测试 toEqual 是正确的值,但测试仍然失败

转载 作者:行者123 更新时间:2023-12-03 12:27:21 26 4
gpt4 key购买 nike

我正在编写一系列测试来演示“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/

26 4 0
文章推荐: javascript - 来自父级
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com