I am testing a service function which makes numerous $http.get()
calls. The actual function under test returns a promise. Currently, the test is failing with response is undefined
.
Here is the test:
it('should return the list of catalogues', inject(function ($q, bookService) {
var list;
var deferred = $q.defer();
var promise = deferred.promise;
promise.then(function (response) {
list = response.success; // Cannot read property 'success' of undefined
});
bookService.getCatalogues().then(function (response) {
deferred.resolve(response); // this line is hit first
});
$httpBackend.flush();
expect(list).toEqual(listOfBooks); // listOfBooks is defined outside test
}));
What am I doing wrong?