I need to unit test some DOM manipulation functions with jasmine (currently I run my tests in the browser and with Karma)
我需要用jasmine单元测试一些DOM操作函数(目前我在浏览器和Karma中运行我的测试)
I was wondering what the best approach would be to do this ?
我想知道这样做的最佳方法是什么?
For example, I can mock and stub the window and document objects and spyOn a couple of their functions. But this doesn't really look like an easy solution, so thats why I'm asking this question!
例如,我可以模拟和存根窗口并记录对象并监视它们的一些功能。但这并不是一个简单的解决方案,所以这就是我问这个问题的原因!
Or is there a better way (not using jasmine maybe) to do this ?
或者是否有更好的方法(不使用茉莉花)来做到这一点?
Thanks a lot
非常感谢
2 个解决方案
#1
28
I've been using a helpful addition to jasmine called jasmine-jquery available on github.
我一直在github上使用一个名为jasmine-jquery的jasmine的有用添加。
It gives you access to a number of useful extra matcher functions, to assert jquery objects and their properties.
它允许您访问许多有用的额外匹配器函数,以断言jquery对象及其属性。
In particular the features I have found useful so far are asserting on attributes of dom elements, and spying on events such as clicks and submits...
特别是到目前为止我发现有用的功能是在dom元素的属性上声明,并且监视诸如点击和提交之类的事件......
Here is a somewhat contrived example... :)
这是一个有点人为的例子...... :)
describe("An interactive page", function() {
it("'s text area should not contain any text before pressing the button", function() {
expect(Page.textArea).toBeEmpty();
});
it("should contain a text area div", function() {
expect(Page.textArea).toBe('div#textArea');
});
it("should append a div containing a random string to the text area when clicking the button", function() {
var clickEvent = spyOnEvent('#addTextButton', 'click');
$('button#addTextButton').click();
expect('click').toHaveBeenTriggeredOn('#addTextButton');
expect(clickEvent).toHaveBeenTriggered();
expect($('div.addedText:last')).not.toBeEmpty());
});
});
and here is the code:
这是代码:
var Page = {
title : 'a title',
description : 'Some kind of description description',
textArea : $('div#textArea'),
addButton : $('button#addTextButton'),
init : function() {
var _this = this;
this.addButton.click(function(){
var randomString = _this.createRandomString();
_this.addTextToPage(randomString);
});
},
addTextToPage : function( text ) {
var textDivToAdd = $('<div>').html('<p>'+text+'</p>');
this.textArea.append( textDivToAdd );
},
createRandomString : function() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
},
};
Page.init();
I've found jasmine to be really flexible and agreeable to use so far, I always appreciate pointers for making it the code better though!
我发现茉莉花非常灵活,并且到目前为止都很适合使用,我总是很欣赏能够让代码变得更好的指针!
#2
3
I was looking for something for myself and finally I made a little library with 19 custom matchers. Maybe you'll find it helpful. https://github.com/devrafalko/jasmine-DOM-custom-matchers
我正在为自己寻找一些东西,最后我用19个自定义匹配器创建了一个小库。也许你会觉得它很有帮助。 https://github.com/devrafalko/jasmine-DOM-custom-matchers
#1
28
I've been using a helpful addition to jasmine called jasmine-jquery available on github.
我一直在github上使用一个名为jasmine-jquery的jasmine的有用添加。
It gives you access to a number of useful extra matcher functions, to assert jquery objects and their properties.
它允许您访问许多有用的额外匹配器函数,以断言jquery对象及其属性。
In particular the features I have found useful so far are asserting on attributes of dom elements, and spying on events such as clicks and submits...
特别是到目前为止我发现有用的功能是在dom元素的属性上声明,并且监视诸如点击和提交之类的事件......
Here is a somewhat contrived example... :)
这是一个有点人为的例子...... :)
describe("An interactive page", function() {
it("'s text area should not contain any text before pressing the button", function() {
expect(Page.textArea).toBeEmpty();
});
it("should contain a text area div", function() {
expect(Page.textArea).toBe('div#textArea');
});
it("should append a div containing a random string to the text area when clicking the button", function() {
var clickEvent = spyOnEvent('#addTextButton', 'click');
$('button#addTextButton').click();
expect('click').toHaveBeenTriggeredOn('#addTextButton');
expect(clickEvent).toHaveBeenTriggered();
expect($('div.addedText:last')).not.toBeEmpty());
});
});
and here is the code:
这是代码:
var Page = {
title : 'a title',
description : 'Some kind of description description',
textArea : $('div#textArea'),
addButton : $('button#addTextButton'),
init : function() {
var _this = this;
this.addButton.click(function(){
var randomString = _this.createRandomString();
_this.addTextToPage(randomString);
});
},
addTextToPage : function( text ) {
var textDivToAdd = $('<div>').html('<p>'+text+'</p>');
this.textArea.append( textDivToAdd );
},
createRandomString : function() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 5; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
},
};
Page.init();
I've found jasmine to be really flexible and agreeable to use so far, I always appreciate pointers for making it the code better though!
我发现茉莉花非常灵活,并且到目前为止都很适合使用,我总是很欣赏能够让代码变得更好的指针!
#2
3
I was looking for something for myself and finally I made a little library with 19 custom matchers. Maybe you'll find it helpful. https://github.com/devrafalko/jasmine-DOM-custom-matchers
我正在为自己寻找一些东西,最后我用19个自定义匹配器创建了一个小库。也许你会觉得它很有帮助。 https://github.com/devrafalko/jasmine-DOM-custom-matchers