【问题标题】:In Jasmine, how does one test a function that uses document.write在 Jasmine 中,如何测试使用 document.write 的函数
【发布时间】:2013-10-11 14:16:25
【问题描述】:

我有一个函数:

var foo = function() {
    document.write( bar() );
};

我的茉莉花测试是:

describe('has a method, foo, that', function() {
    it('calls bar', function() {
        spyOn(window, 'bar').andReturn('');
        foo();
        expect(bar).toHaveBeenCalled();
    });
});

我的问题是测试通过并且foo document.writes 到页面,完全覆盖了页面。有什么好的方法可以测试这个功能吗?

A related issue

【问题讨论】:

    标签: javascript unit-testing jasmine


    【解决方案1】:

    你可以监视document.write

    var foo = function () {
      document.write('bar');
    };
    
    describe("foo", function () {
    
      it("writes bar", function () {
        spyOn(document, 'write')
        foo()
        expect(document.write).toHaveBeenCalledWith('bar')
      });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 2017-01-16
      • 1970-01-01
      • 2018-11-15
      • 2016-12-18
      • 2015-10-21
      • 1970-01-01
      相关资源
      最近更新 更多