2

Doing AngularJS unit tests using the $httpBackend service. For some reason $httpBackend.expectGET('') or $httpBackend.expectGET() seem to be working like a catch-all for the request coming from the tested code i.e. Karma says the test passes with SUCCESS.

More precisely, if I mock the backend with:

$httpBackend.expectGET('').respond(200,'all good');

The tested angular code that makes the $http.get('specific/url/here') call will get captured by this empty `$httpBackend.expectGET('').

I know it is this one that catches the request because if I instead mock the backend with:

$httpBackend.expectGET('bad/url/here').respond(200,'all good');

then Karma FAILs with message:

Error: Unexpected request: GET specific/url/here
Expected GET bad/url/here

Is that expected behavior?

I don't see something relevant in the docs.

UPDATE:

Noticed something very weird. It doesn't make any difference if the expectGET() has the url parameter passed or not. What matters is the order in which the $httpBackend.expectGET() are declared and it needs to be the same as the order in which the requests are coming in from the application code.

For example if the mocked requests are:

$httpBackend
    .expectGET(URL_A)
    .respond({
        teams: [{
            team_id: 6,
            team_name: 'foo'
        }]
    });
$httpBackend
    .expectGET(URL_B)
    .respond({
        data: [{
            data_id: 1
        }]
    });

and the order in which the requests arrive from the app are first for URL_A and then for URL_B, the test passes regardless of whether we specify the URLs explicitly or not.

But if we change the order of the mocked requests, then the test fails.

Once again I don't see this point being made in the docs.

0 Answers0