|
|
|||||
Another Way to Test Ajax MethodsA while ago, Chad posted about how to test Ajax methods with QUnit. I’m also testing Ajax calls, but taking a different approach I thought I would share. Chad’s approach was to put a timer in the test and waiting a while before performing the assertion. Another way is to hook into JQuery global ajax events, like the ajaxComplete event. Here’s some sample code that makes a call to the Flickr API via jsonp. var url = "http://api.flickr.com/services/feeds/photos_public.gne?tags=austin,tx&format=json&jsoncallback=?"; $(document).ready(function(){ }); Avoid Testing Ajax Calls in Unit Tests Huh? You just showed me a way to do it, why should I avoid it? Making an Ajax call is just like making a database call, which should only be done for integration style tests. Just as you would stub a data repository method call in C#, you should do the same for AJAX calls. Except in JavaScript, this is 10 time easier because of it’s a dynamic, functional language. You can easily stub out method calls by replacing the method. In this case I am testing against the title property, so I need a stub that looks like the getJSON method and returns back the title property. Here is the same test, but with a stubbed out getJSON method. var stubbedJSON = function(url,data,callback,format){ source: lostechies |
|||||
| Copyrights Reserved AjaxProjects.com 2006-2013, Powered by Enozom - Mobile Development Company -Privacy Policy |