I'm currently working on testing a service function that involves multiple $http.get()
calls. The function being tested returns a promise but the test is failing with an error stating response is undefined
.
Below is the test script:
it('should retrieve 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 executed first
});
$httpBackend.flush();
expect(list).toEqual(listOfBooks); // listOfBooks is defined outside the test
}));
Any idea what could be causing this issue?