gpt4 book ai didi

javascript - 在 Jasmine 中,如何测试使用 document.write 的函数

转载 作者:行者123 更新时间:2023-11-29 16:14:24 25 4
gpt4 key购买 nike

我有一个函数:

var foo = function() {
document.write( bar() );
};

我的 Jasmine 测试是:

describe('has a method, foo, that', function() {
it('calls bar', function() {
spyOn(window, 'bar').andReturn('');
foo();
expect(bar).toHaveBeenCalled();
});
});

我的问题是测试通过并且 foo document.writes 到页面,完全覆盖页面。有什么好的方法可以测试这个功能吗?

A related issue

最佳答案

你可以监视 document.write

var foo = function () {
document.write('bar');
};

describe("foo", function () {

it("writes bar", function () {
spyOn(document, 'write')
foo()
expect(document.write).toHaveBeenCalledWith('bar')
});
});

关于javascript - 在 Jasmine 中,如何测试使用 document.write 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19320422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com