在 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");
});
相关文章