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

Javascript 在Jasmine中,如何测试使用document.write的函数,javascript,unit-testing,jasmine,Javascript,Unit Testing,Jasmine,我有一个功能: var foo = function() { document.write( bar() ); }; 我的茉莉花测试是: describe('has a method, foo, that', function() { it('calls bar', function() { spyOn(window, 'bar').andReturn(''); foo(); expect(bar).toHaveBeenCalle

我有一个功能:

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.write写入页面,完全覆盖页面。有没有一个好的方法来测试这个功能


您可以监视
文档。编写

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

describe("foo", function () {

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