admin管理员组文章数量:1334133
I am new to javascript testing. I am using jasmine and need to test if correct arguments have been passed to a method.
This is my method:
function myView(){
if($('.view').is('.list')){
myWindow('list');
}else{
myWindow('random');
}
$('.view').toggleClass('my-list');
}
function myWindow(list) {
var url = /test.json;
$.post(url, {"list": list});
}
Here are my tests:
describe('#myView', function() {
beforeEach(function() {
fixture.load('myview.html');
});
it('sets window to list', function(){
expect(window.myWindow).toHaveBeenCalledWith('list');
});
});
I get the following error.
Error: Expected a spy, but got Function.
If i add this line before the expect(which seems wrong because I am specifying the correct param which should be identified by test)
spyOn(window, myWindow('list'));
I get the following error:
undefined() method does not exist
Can someone show my a good way to write the above tests?
I am new to javascript testing. I am using jasmine and need to test if correct arguments have been passed to a method.
This is my method:
function myView(){
if($('.view').is('.list')){
myWindow('list');
}else{
myWindow('random');
}
$('.view').toggleClass('my-list');
}
function myWindow(list) {
var url = /test.json;
$.post(url, {"list": list});
}
Here are my tests:
describe('#myView', function() {
beforeEach(function() {
fixture.load('myview.html');
});
it('sets window to list', function(){
expect(window.myWindow).toHaveBeenCalledWith('list');
});
});
I get the following error.
Error: Expected a spy, but got Function.
If i add this line before the expect(which seems wrong because I am specifying the correct param which should be identified by test)
spyOn(window, myWindow('list'));
I get the following error:
undefined() method does not exist
Can someone show my a good way to write the above tests?
Share Improve this question edited Oct 22, 2013 at 3:04 newbie asked Oct 18, 2013 at 2:57 newbienewbie 1,0331 gold badge22 silver badges48 bronze badges 1- This could be causing an issue: myWinodw('list'); – m3h2014 Commented Oct 18, 2013 at 3:02
1 Answer
Reset to default 5 +100spyOn
's second parameter is the name of the property you need to spy on. When you call spyOn(window, myWindow('list'));
, your second parameter is the return value of myWindow('list')
which is undefined
=> throws error: undefined() method does not exist
In your code, simply doing this should work:
describe('#myView', function() {
beforeEach(function() {
fixture.load('myview.html');
});
it('sets window to list', function(){
spyOn(window, "myWindow");//spy the function
myView();//call your method that in turn should call your spy
expect(window.myWindow).toHaveBeenCalledWith('list');//verify
});
});
In software unit testing, there are concepts called stub and mock objects. These are dependencies of the method under test. spyOn
is to create your fake objects to test your methods.
You're accessing the global window
object directly, that's really a problem in unit testing. Although Javascript is a dynamically typed language, we're still able to mock your window
object (this is not possible with some statically-typed languages like c#). But to create a good unit-testable code, I remend you should redesign your code to inject it from outside.
function myView(awindow){ //any dependency should be injected, this is an example to inject it via parameter
if($('.view').is('.list')){
awindow.myWindow('list');
}else{
awindow.myWindow('random');
}
$('.view').toggleClass('my-list');
}
Try this:
describe('#myView', function() {
beforeEach(function() {
fixture.load('myview.html');
});
it('sets window to list', function(){
var spy = {myWindow:function(list){}};
spyOn(spy, "myWindow"); //create a spy
myView(spy); //call your method that in turn should call your spy
expect(spy.myWindow).toHaveBeenCalledWith('list'); //verify
});
});
One more thing, jQuery code like this is not quite a good candidate for unit testing as it involves DOM manipulation in your code. If you have time, you should take a look at angularjs framework that separates your View (DOM) from your model (logic), uses dependency injection to make your code testable.
版权声明:本文标题:javascript - jasmine jquery test to check if correct arguments have been passed to a method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742347460a2457814.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论