2013-10-11 21 views
1

功能我有一個函數:在茉莉花,一個人如何測試使用文件撰寫

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

回答

4

可以窺探document.write

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

describe("foo", function() { 

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