8

I'm trying to test that my program processes the error messages I get from http requests properly, but I can't figure out how to mock that with $httpBackend. Given the following:

$httpBackend.expectGET(path).respond(400, object);

400 sets response.status, and object sets response.data, but how do I set response.error? The data I need to test isn't in the data field.

Example API response:

{
    status: 400,
    data: {},
    error: {}
}
5
  • I have the same question. Commented Mar 30, 2016 at 16:31
  • There is no error in the response objects. Post your code and clarify what you want to test. Commented Mar 30, 2016 at 16:39
  • There is no error in the httpBackend response objects, but there is in the objects I get from the API I'm using. That's the problem. Commented Mar 30, 2016 at 17:01
  • So, your backend returns a response whose body is an object with a status data and error fields? If so, make sure that object, in your code, is such an object. Commented Mar 30, 2016 at 18:05
  • What are you putting in object? Commented Apr 15, 2016 at 19:01

2 Answers 2

4

the code ...

$httpBackend.expectGET(path).respond(400, object);

is saying when the code makes a request to the 'path' endpoint, respond with 400 status code with the response 'object'

so, for example, lets say that your unit test hits this code in your app ...

$http.GET(path).then(function(response) {
    // this code is hit with 2xx responses
    vm.fred = 'hello';
}).catch(function(errorResponse) {
    // this code is hit with any status code not starting with 2! e.g. 400
    vm.fred='failed';
});

Thus your code will cause the catch block to be executed. However, inside your unit test, to force the execution of the catch block you will need to do ...

$rootScope.$digest();

this will trigger the execution of the then or catch blocks (whichever is relevant).

you can then make expectations in your unit test as usual. Note that in your example errorResponse will be object.

Sign up to request clarification or add additional context in comments.

1 Comment

Perhaps I didn't word my question very well, but this does not answer it. The problem is that $httpBackend will put the object I pass it in errorResponse.data when I need it to be in errorResponse.error to mock the API in question.
0

respond method on $httpBackend chain doesn't offer the way to customize the response. It can be customized with $http interceptors. The most simple way to specify an error for each response is using a global:

var responseError;

beforeEach(module('app', ($httpProvider) => {
  function responseErrorInterceptor($q) {
    return {
      responseError: (response) => {
        response.error = responseError;
        return $q.reject(response);
      }
    };
  }

  $httpProvider.interceptors.push(responseErrorInterceptor);
}));

...      
responseError = { ... };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.