I have build a web site with jQuery and lots of ajax request (json format).
I would like to make some unit test to verify request on server side. As I used jQuery, I use qUnit, but I have a problem of order of test...For example, I wanted to test this: - create a user => could be possible
- rename the user with a valid name => could be possible - rename the user with a used name => could not be possible - remove the user => could be possibleMy code:
$("button#test").button().click(function() {
module("Module Users");
newName = 'newUserName';
userId = 0;
test("1 Add a user", function() {
stop();
$.getJSON(Request,{'action':'add','table':'users'}
,function(data) {
equal( data.status,"OK", "Answer is OK" );
notEqual( data.item,null, "item is return" );
userId = data.item.id;
start();
});
});
test("2 Rename user", function() {
stop();
$.getJSON(Request,{'action':'modify','table':'users','id':userId,'field':'name','value':newName}
,function(data) {
equal( data.status,"OK", "Answer is OK" );
equal( data.value,newName, "Return value is OK" );
start();
});
});
test("3 Rename user with use name", function() {
stop();
badName = 'usedName'; // assert that a user with this name exists
$.getJSON(Request,{'action':'modify','table':'users','id':userId,'field':'name','value':badName}
,function(data) {
equal( data.status,"Fail", "Answer is Fail" );
equal( data.value,newName, "Return value is previous name" );
start();
});
});
test("4 Remove the user", function() {
stop();
$.getJSON(Request,{'action':'remove','table':'users','id':userId}
,function(data) {
equal( data.status,"OK", "Answer is OK" );
s开发者_StackOverflow社区tart();
});
});
But the problem is that the 1 test is run, then the 4 and the 2 and 3... (Then, I think the problem is that my tests are not independant)
How it is possible to solve this ?
I can cascade all the 4 tests in 1 but I think it will be less readable...What do you think ?
Sometimes you just want to get the job done If needs must, try.
QUnit.config.reorder = false;
The main problem in your example is that the tests are run within the click event handler. You need to refactor that and make the calls to test() on the top level (independent of any click event). As your tests themselve only test ajaxy functionality, you shouldn't have to use the button at all. So something like this:
test("1 Add a user", function() {
stop();
$.getJSON(Request,{'action':'add','table':'users'}
,function(data) {
equal( data.status,"OK", "Answer is OK" );
notEqual( data.item,null, "item is return" );
userId = data.item.id;
start();
});
});
test("2 Rename user", function() {
stop();
$.getJSON(Request,{'action':'modify','table':'users','id':userId,'field':'name','value':newName}
,function(data) {
equal( data.status,"OK", "Answer is OK" );
equal( data.value,newName, "Return value is OK" );
start();
});
});
test("3 Rename user with use name", function() {
stop();
badName = 'usedName'; // assert that a user with this name exists
$.getJSON(Request,{'action':'modify','table':'users','id':userId,'field':'name','value':badName}
,function(data) {
equal( data.status,"Fail", "Answer is Fail" );
equal( data.value,newName, "Return value is previous name" );
start();
});
});
test("4 Remove the user", function() {
stop();
$.getJSON(Request,{'action':'remove','table':'users','id':userId}
,function(data) {
equal( data.status,"OK", "Answer is OK" );
start();
});
});
As kelloti says, qUnit is for unit testing and "unit tests are supposed to be independent and isolated from each other"... Then I have to add an element before testing the removing.
精彩评论