Testing DOM manipulating in Jasmine test(在 Jasmine 测试中测试 DOM 操作)
问题描述
我正在创建一个 js 小部件,第一部分是添加脚本宽度 javascript,如下所示(来自谷歌分析的示例):
I'm creating a js widget and first part is to add script width javascript, something like this (example from google analytics):
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
如何用 jasmine 测试它(使用夹具?)?
How to test it with jasmine (using fixtures?)?
推荐答案
为了在我的规范中设置 HTML 固定装置,我写了 茉莉花装置.有了它,你可以做这样的事情:
For setting up HTML fixtures in my specs, I wrote jasmine-fixture. With it, you can do stuff like this:
var $foo, $input;
beforeEach(function(){
$foo = affix('.foo');
# appends to the DOM <div class="foo"></div>
$input = $foo.affix('input[id="name"][value="Jim"]');
# appends <input id="name" value="Jim"/> beneath the .foo div
而且 afterEach,它会在你之后清理.
And afterEach, it'll clean up after you.
对于 DOM 状态的预期,我使用 jasmine-jquery.它提供了大量的匹配器,如下所示:
For expectations about the state of the DOM, I use jasmine-jquery. It offers a ton of matchers like the one below:
it('is named Jim', function(){
expect($input).toHaveValue("Jim");
});
这篇关于在 Jasmine 测试中测试 DOM 操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Jasmine 测试中测试 DOM 操作


- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01