最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - In Jasmine, how does one test a function that uses document.write - Stack Overflow

programmeradmin1浏览0评论

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, pletely overwriting the page. Is there a good way to test this function?

A related issue

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, pletely overwriting the page. Is there a good way to test this function?

A related issue

Share Improve this question edited May 23, 2017 at 12:06 CommunityBot 11 silver badge asked Oct 11, 2013 at 14:16 jdsjds 8,28013 gold badges68 silver badges111 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

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')
  });
});
发布评论

评论列表(0)

  1. 暂无评论