我有一个功能:
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到页面,完全覆盖了页面。有没有一种很好的方法来测试这个功能?
答案 0 :(得分: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')
});
});