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

标签 javascript jasmine

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

相关文章:

javascript - 如何从 Jasmine 测试 Angular 2/4 触发文档级事件

unit-testing - Jasmine 中 and.stub 与 and.callFake 有什么区别

javascript - 如何从回调函数返回两个数字中最大的一个?

javascript - `(?=regex)` 和 `(?!regex)` 可以用在正则表达式中间吗?

javascript - 单击链接时更改 session 变量

javascript - 在 Node.js 附加代码中使用 Nan 创建数组

php - Cronjob 但适用于 jQuery/Javascript

javascript - Jasmine toThrowError 失败,消息 "Error: Actual is not a function"

javascript - 在集成测试的规范之间重启浏览器是否可以?

angular - ng-bootstrap Angular 配置 karma Jasmine