Wednesday, October 30, 2013

AngularJS Unit testing - A helper for stubbing dependent methods that return promises

Deferreds and Promises have changed javascript development for the better. They offer an escape from callback hell. AngularJS supports a version of promises heavily inspired by the popular q library.

This is great however testing mocking out angularjs service dependencies which return promises was less great!

I want to test this:

Incidentally this kind of pattern is becoming more common now promise unwrapping is gone.

What I wanted:
  • A way to easily mock the "fetchData" service method
  • Capability to assert "fetchData" was called the with right arguments ( using jasmine spies)
  • The mock "fetchData" should return a promise so I can call "then" and use utilities such as $q.all
  • As little boiler plate code as possible
So I wrote this little helper:

And here is what the full test looks like:

Here is a full working jsfiddle

There's potential to expand this out having more functionality such as dealing with failing promises and giving more control over when the promise is resolved etc. If you think that's a good idea let me know in the comments.