【问题标题】:Jasmine Create Spy on Native JS Function on Global Window ObjectJasmine 在全局窗口对象上的本机 JS 函数上创建间谍
【发布时间】:2015-05-02 04:17:16
【问题描述】:

我正在编写一个测试以查看是否调用了 Array.prototype.map。我认为这会起作用,因为 Array.prototype.map 位于全局窗口对象上:

    it("does not use Array.prototype.map", function(){

        spyOn(window, "Array.prototype.map")
        fn([2,2,3]);
        expect(Array.prototype.map.calls.count()).toEqual(0);
    });

我收到错误Array.prototype.map does not exist。当我创建自己的自定义全局函数时,此方法可以正常工作。 Based on this other post 使用我上面使用的语法似乎可以监视任何全局函数。如果我创建自己的函数,则此语法有效。关于为什么 Array.prototype.map 返回未定义的任何想法?

【问题讨论】:

    标签: javascript jasmine


    【解决方案1】:

    希望你已经得到了答案,但是对于搜索的人来说,这是因为你应该

    spyOn(Array.prototype, 'map');
    

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-15
      相关资源
      最近更新 更多