问题
I have a function:
var foo = function() {
document.write( bar() );
};
My Jasmine test is:
describe('has a method, foo, that', function() {
it('calls bar', function() {
spyOn(window, 'bar').andReturn('');
foo();
expect(bar).toHaveBeenCalled();
});
});
My problem is that the test passes and foo
document.writes to the page, completely overwriting the page. Is there a good way to test this function?
A related issue
回答1:
You can spy on document.write
var foo = function () {
document.write('bar');
};
describe("foo", function () {
it("writes bar", function () {
spyOn(document, 'write')
foo()
expect(document.write).toHaveBeenCalledWith('bar')
});
});
来源:https://stackoverflow.com/questions/19320422/in-jasmine-how-does-one-test-a-function-that-uses-document-write