Jasmine is a great testing framework but when you have to deal with promises is not always clear how. Typical use cases are asynchronous services calls to a backend. For instance, consider the following service:
EmployeeService.get() returns a promise that will be either fulfilled or rejected. Testing a promise without the necessary guards can lead to two different scenarios.
First we get a deferred pass or fail when running our test suite, this can make tracing failed tests very difficult. Second possibility and more dangerous one, our test suite finishes before the promise is completed hiding passes and failures under the rag.
Since Jasmine 2.0, test methods can be supplied with an special function called
done. The test runner will wait until this function is called before moving to the next test, it also sets a watchdog in case it is never called.
In this case
http.flush() will also force the test runner to wait before the promise is completed. But this is a particularity of
$httpBackend, it is generally safer to use
The other particularity is that we also created a
failTest function that upon invocation will signal a test failure, without this, if for any reason the promise is broken our test will silently pass.