在Jasmine中,如何测试使用document.write的函数

gwg*_*gwg 2 javascript unit-testing jasmine

我有一个功能:

var foo = function() {
    document.write( bar() );
};
Run Code Online (Sandbox Code Playgroud)

我的Jasmine测试是:

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

我的问题是测试传递和foodocument.writes到页面,完全覆盖页面.有没有一种好方法来测试这个功能?

相关问题

And*_*rle 6

你可以窥探 document.write

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

describe("foo", function () {

  it("writes bar", function () {
    spyOn(document, 'write')
    foo()
    expect(document.write).toHaveBeenCalledWith('bar')
  });
});
Run Code Online (Sandbox Code Playgroud)