When writing qUnit, it often gets hard trying to simulate an ajax operation.
Often you find yourself writing passing tests, but then breaking them when you actually have to provide the data service, or worse tightly coupling them to the actual ajax calls, or hack apart your javascript just to write tests. This doesn’t have to happen.
I have written a short helper to allow us to test async.
How does it work?
Imagine you have a very simple bit of code that shows results from the data set
Now we should be writing qUnit tests to assert that it works in all scenarios.
Here are two such tests:
As you can see, we can mock out data request really easily with the style of data service. You simulate an existing Data service and get reliable output so you can test every scenario. This means there is no excuse to write code for if your data services fail to return with a success message - it’s a very simple task to write code to test for the failing data service scenarios.