I regularly run multiple test on the same function. This will involve having a function to set up the tests and then specifying the behaviour of stubs etc.
What I'm finding is that the tests run fine by themselves, but when they are run alltogether they fail. It is as if sinonjs is sharing the setup functions return values and one test is changing the setup of another.
Example:
function getSaveFieldFromFormTester( ){
jQuery( '#system_templates').html( testHtmlOfflineHandler );
var holder = getOfflineHandlerAndXhrSandbox();
var spy = holder.sinon.spy(jQuery.prototype, 'not');
var stub2 = holder.sinon.stub(holder.o, 'setData' );
holder.o.inject( 'setData', stub2 );
return [holder, spy, stub2];
}
test ("saveFieldFromFormNewAndNotFound", function()
{
var tester = getSaveFieldFromFormTester();
tester[0].o.saveFieldFromForm.defaults = { name: 'theName' };
tester[1].withArgs( '#actions_edit .rowwrapper[data-id!="template"] .name .widget :input' );
tester[0].o.saveFieldFromForm('actions', 55, 55, 'nameOfFieldThatDoesntExist', true );
equal( tester[1].callCount, 0, 'field not found, setData not called' );
equal( tester[1].callCount, 1, 'right jQuery selector used');
tester[1].restore();
tester[2].restore();
jQuery.prototype.not.restore();
});
test ("saveFieldFromFormExistingCheckboxSet", function()
{
var tester = getSaveFieldFromFormTester();
jQuery( '#actions_edit .rowwrapper[data-id="567"] .completed .widget :input').prop( 'checked', true );
tester[1].withArgs( '#actions_edit .rowwrapper[data-id!="567"] .completed .widget :input' );
tester[2].withArgs( [ 'actions', 567, 'completed', 1 ]);
tester[0].o.saveFieldFromForm('actions', 567, 567, 'completed', false );
equal(tester[1].callCount, 1, 'right jQuery selector used');
equal(tester[2].callCount, 1, 'setData called with right Data');
tester[1].restore();
tester[2].restore();
jQuery.prototype.not.restore();
});
test ("saveFieldFromFormExistingCheckboxUnSet", function()
{
var tester = getSaveFieldFromFormTester();
jQuery( '#actions_edit .rowwrapper[data-id="567"] .completed .widget :input').prop( 'checked', false );
tester[1].withArgs( '#actions_edit .rowwrapper[data-id!="567"] .completed .widget :input' );
tester[2].withArgs( [ 'actions', 567, 'completed', 0 ]);
tester[0].o.saveFieldFromForm('actions', 567, 567, 'completed', false );
equal(tester[1].callCount, 1, 'right jQuery selector used');
equal(tester[2].callCount, 1, 'setData called with right Data');
tester[1].restore();
tester[2].restore();
jQuery.prototype.not.restore();
});
Output:
saveFieldFromFormNewAndNotFound (2, 1, 3)
field not found, setData not called
Expected:
0
Result:
1
Diff:
01
Source:
at http://ift.tt/1M4ljL6
right jQuery selector used@ 39 ms
Died on test #3 at QUnit.test.global.test (http://ift.tt/1Qj303e)
at runJSUnitTestsOfflineHandler (http://ift.tt/1M4ljL8)
at HTMLDocument.<anonymous> (http://ift.tt/1Qj303i)
at k (http://ift.tt/1M4liqp)
at Object.l.fireWith [as resolveWith] (http://ift.tt/1Qj303r)
at Function._.extend.ready (http://ift.tt/1M4lk1o)
at HTMLDocument.g (http://ift.tt/1Qj303u): jQuery.prototype.not.restore is not a function
Source:
TypeError: jQuery.prototype.not.restore is not a function
at Object.<anonymous> (http://ift.tt/1M4liqs)
at Object.sinonSandboxedTest (http://ift.tt/1Qj2YII)
saveFieldFromFormExistingCheckboxSet (1, 2, 3)Rerun16 ms
right jQuery selector used@ 16 ms
setData called with right Data@ 16 ms
Died on test #3 at QUnit.test.global.test (http://ift.tt/1Qj303e)
at runJSUnitTestsOfflineHandler (http://ift.tt/1M4lk1t)
at HTMLDocument.<anonymous> (http://ift.tt/1Qj303i)
at k (http://ift.tt/1M4liqp)
at Object.l.fireWith [as resolveWith] (http://ift.tt/1Qj303r)
at Function._.extend.ready (http://ift.tt/1M4lk1o)
at HTMLDocument.g (http://ift.tt/1Qj303u): jQuery.prototype.not.restore is not a function@ 16 ms
Source:
TypeError: jQuery.prototype.not.restore is not a function
at Object.<anonymous> (http://ift.tt/1Qj303w)
at Object.sinonSandboxedTest (http://ift.tt/1Qj2YII)
saveFieldFromFormExistingCheckboxUnSet (1, 2, 3)Rerun8 ms
right jQuery selector used
setData called with right Data
Died on test #3 at QUnit.test.global.test (http://ift.tt/1Qj303e)
at runJSUnitTestsOfflineHandler (http://ift.tt/1M4lk1v)
at HTMLDocument.<anonymous> (http://ift.tt/1Qj303i)
at k (http://ift.tt/1M4liqp)
at Object.l.fireWith [as resolveWith] (http://ift.tt/1Qj303r)
at Function._.extend.ready (http://ift.tt/1M4lk1o)
at HTMLDocument.g (http://ift.tt/1Qj303u): jQuery.prototype.not.restore is not a function@ 8 ms
Source:
TypeError: jQuery.prototype.not.restore is not a function
at Object.<anonymous> (http://ift.tt/1Qj30jN)
at Object.sinonSandboxedTest (http://ift.tt/1Qj2YII)
Aucun commentaire:
Enregistrer un commentaire