Fetching templates in tests using jqUnit

Michelle D'Souza michelle.dsouza at utoronto.ca
Thu Jul 16 21:03:05 UTC 2009


>
> If you look at the code for 'fetchTemplate' you'll notice the use of  
> 'stop' and 'start'. These are QUnit functions that pause the test  
> runner while waiting for the AJAX response. The way to use them is  
> to call 'stop' prior to your ajax call and call 'start' in the ajax  
> callback.


Thought I'd give a little more information on using 'stop' and  
'start'. There are 4 lifecycle phases in the test: setup, testbody,  
teardown, reset. If stop is called in one of the lifecycle phases,  
that lifecycle phase continues until it is done but the next lifecycle  
phase will not begin until 'start' has been called. In the following  
example foo and bar will be run before the ajax call returns but baz  
will only run after the 'start' call in the ajax callback.

function setup () {
	stop();
	foo();
	var ajaxCallback = function () {
		start();
	};
	ajax(ajaxCallback);
	bar();
}

function testBody () {
	baz();
}

The implication here is that if you want to run some tests after the  
ajax call has returned, you need to put those tests into the callback.  
If you want to do another ajax call after running those tests, you  
need to put the ajax call into the callback also like this:

function setup () {
	stop();
	foo();
	var ajaxCallback = function () {
		blah();
		var secondAjaxCallback = function ()
			start();
		}
		ajax(secondAjaxCallback);
	};
	ajax(ajaxCallback);
	bar();
}

function testBody () {
	baz();
}

In the second example, foo and bar run before any ajax calls return.  
'blah' runs after the first ajax call and 'baz' runs after the second  
(nested) ajax call.

Hope this helps.

Michelle

------------------------------------------------------
Michelle D'Souza
Software Developer, Fluid Project
Adaptive Technology Resource Centre
University of Toronto



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.idrc.ocad.ca/pipermail/fluid-work/attachments/20090716/b1f5a80c/attachment.htm>


More information about the fluid-work mailing list