jds
jds

Reputation: 8279

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

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

Upvotes: 2

Views: 4241

Answers (1)

Andreas Köberle
Andreas Köberle

Reputation: 111062

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')
  });
});

Upvotes: 6

Related Questions