These days I found that QUnit lacks of one functionality the like as assert_difference in Rails testing, which is very useful when keeping track on specified property for expected increment.
Traditionally one want to testing if the code respond correctly by incrementing 1 in the length property after performing a remove operation, the following code may be used:
var size_before_remove = Library.length;
Library.remove( something ... );
equals ( size_before_remove - Library.length, 1, "increment" );
I encapsulated this kind of behaviors into a function called difference, as follows:
$.extend( window, {
difference: function() {
var subject = arguments[0],
expect = parseInt( arguments[1] ), expect = ( isNaN( expect ) ) ? 1 : expect,
fn = arguments[2] || arguments[1];
equals ( 0 - eval( subject ) + (function() {
fn();
return eval( subject );
})(), expect, "assert difference" );
}
} );
Put it before the test case script and invoke it in the test case as the following ways:
difference( 'object.length', 1, function() {
// doing something that will change the object.length
}
or
difference( 'object.length', function() { } ) // with expected increment equals 1
Maybe it is the fact that QUnit doesn't need assert_difference due to the chainnability of jQuery library. For example, one can use the following code to testing if the size in one object correctly increment by 1 after inserting one node.
equals ( 0 - $('body > *').size() + $('body').append('<li>hi</li>').children().size(), 1, 'increment' );
0 意見:
張貼意見