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 possible
My 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" );
start();
});
});
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;