使用 QUnit 测试使用 document.write() 的实用函数:使文档对象成为其他文档?
我有一对实用函数,它们使用 document.write()
来更改 DOM,注入加载 JavaScript 和 CSS 的标签。
是否可以使用 QUnit 来测试注入是否正确发生,而不影响测试结果页面的实际 DOM?
我可以:
覆盖
document.write()
,也许在setup
中,并在teardown
中重置它,成为我定义的函数。但我并没有测试对document.write()
的调用是否执行了它们应该执行的操作。使用 js-test-driver 之类的东西让 JavaScript 从命令行运行(尽管我真的希望测试结果页面可用)。
仅为这些实用程序测试创建一个单独的测试结果页面,以便这些结果不会以任何方式、形状或形式影响其他测试(尽管我确实想要一个统一的页面)。
传递实用函数空 JS 和 CSS 文件来加载,这样它们实际上不会影响结果页面。
所以我有选择。
但我真正想要的是使文档
成为实际当前文档之外的某个文档。我不确定这是否可能。 (事实上,我什至不确定这是否有意义。)这可能吗?这似乎是我可以用 iframe 或类似的东西来做的事情。
如果有帮助的话,这是我想要进行单元测试的函数类型的示例:
function(jsFile){
document.write('<script type="text/javascript" src="'+jsFile+'"></script>');
}
I have a pair of utility functions that use document.write()
to alter the DOM, injecting tags that load JavaScript and CSS.
Is it possible to use QUnit to test that the injection is happening correctly without having it impact the actual DOM of the test results page?
I can:
Overwrite
document.write()
, perhaps insetup
and resetting it inteardown
, to be a function I define. But them I'm not testing that the calls todocument.write()
do what they're supposed to do.Use something like js-test-driver to have the JavaScript run from a command line (although I really want to have the test results page available).
Create a separate test results page just for these utility tests so that those results don't affect the other tests in any way, shape, or form (although I really want a single unified page).
Pass the utility functions empty JS and CSS files to load so they don't actually impact the results page.
So I have options.
But what I really want is to make document
be some document other than the actual current document. I'm not sure this is possible. (In fact, I'm not even sure it makes any sense.) Is this possible? It seems like something I might be able to do with an iframe or something like that.
If it helps at all, here's an example of the type of function I want to unit test:
function(jsFile){
document.write('<script type="text/javascript" src="'+jsFile+'"></script>');
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我意识到我不需要测试
document.write()
。我只需要测试我的代码是否正在向document.write()
发送预期的内容。因此我选择了上面的第一个选项,在setup()
和teardown()
中暂时重新定义document.write()
进行测试。I realized that I don't need to test
document.write()
. I merely need to test that my code is sending something expected todocument.write()
. So I went with the first option above, redefiningdocument.write()
temporarily in thesetup()
andteardown()
for the test.