Angular $httpBackend.expectGET - respond with 409 status & custom statusText

angularjs, javascript

Solution

The documentation says:

respond – `{function([status,] data[, headers, statusText]) | function(function(method, url, data, headers)}` – The respond method takes a set of static data to be returned or a function that can return an array containing response status (number), response data (string), response headers (Object), and the text for the status (string).

You're doing the second option, passing a function that returns an array. Here's what should work. Status text is the 4th item. (For clarity, I included the optional function parameters.)

$httpBackend
.expectGET('/example/url')
.respond(function (method, url, data, headers) {
    return [409, 'response body', {}, 'TestPhrase'];
});

Problem

Trying to create a $httpBackend.expectGET in a unit test to return a status of 409 and a custom statusText of 'TestPhrase'. Looking at the Angular docs (https://docs.angularjs.org/api/ngMock/service/$httpBackend) it gives the following example of what is returned: {function([status,] data[, headers, statusText]) | function(function(method, url, data, headers)} Having trouble understanding how to interpret the above example. My current code is: ``` $httpBackend .expectGET('/example/url') .respond(function () { return ['409', ['TestPhrase'], {}]; }); ``` The code i'm testing is: ``` $http.get('/example/url').then(function (response) { console.log('success, status ' + response.status); console.log('success, statusText ' + response.statusText); }, function(response) { console.log('error, status ' + response.status); console.log('error, statusText ' + response.statusText); } }); ``` The output to console I am receiving from this test is: ``` 'error, status 409' 'error, statusText ' ``` Expected output is: ``` 'error, status 409' 'error, statusText TestPhrase' ```

Original source