In Jasmine, how does one test a function that uses document.write

我只是一个虾纸丫 提交于 2019-12-13 16:06:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!