在 Jasmine 测试中测试 DOM 操作 [英] Testing DOM manipulating in Jasmine test

查看:23
本文介绍了在 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 操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆