0

I'm using unit-testing in my angularJS application. Here's a test spec I have for a service:

describe('service', function () {
    var service = {};
    var $httpBackend;

    beforeEach(module('myApp'));
    beforeEach(inject(function (_service_, _$httpBackend_) {
        service = _service_;
        $httpBackend = _$httpBackend_;
    }));

    it('should return test message', function () {
        var response;
        var test = "This is a test";

        $httpBackend.when('GET', '/api/Account/GetTest')
            .respond(200, test);

        service.getTest().then(function (data) {
            response = data;
        });

        expect(response).toEqual(test);
    });
});

And, here's the getTest function in my service:

var getTest = function () {
    return $http.get("api/Account/GetTest");
};

Why am I getting the following error:

Error: Unexpected request: GET api/Account/GetTest

I also figured if I remove the slash from the url in my spec, the error will change to this (and I have no idea why):

Error: Unexpected request: GET /app/views/login.html

ataravati
  • 8,891
  • 9
  • 57
  • 89

1 Answers1

0

The correct test would be to remove the slash. The error that you get then:

Error: Unexpected request: GET /app/views/login.html

implies that angular is trying to download a template unexpectedly. But, since you are using the mock $http, you are getting an error because of the unexpected request.

You could add something like this:

$httpBackend.when('GET', '/app/views/login.html')
        .respond(200, '');

And the error might go away, but:

  1. This may uncover other unexpected template retrievals
  2. Your test is showing unexpected behavior and is therefore unlikely to be small enough to be considered a unit test.

I would recommend that you look through your code and figure out why the login template is being loaded (most likely, you are inadvertently loading a directive or component). Then you can determine if it should indeed be part of the test, or if you should stub it out somehow.

You may need to do this for other templates as well.

Andrew Eisenberg
  • 28,387
  • 9
  • 92
  • 148
  • It looks like the routing is interfering with it, or the code I have in the `routeChangeStart` event. Did you see this post? https://stackoverflow.com/questions/23655307/ui-router-interfers-with-httpbackend-unit-test-angular-js/23670198#23670198 – ataravati Mar 05 '18 at 21:08
  • It might be that the routing is interfering with the test, and that is exactly the point that I am making. I'm guessing that you have some auth logic that is running before your route. In your test, you should be mocking it out so that you are not accidentally invoking unrelated behavior. – Andrew Eisenberg Mar 05 '18 at 21:28
  • How do I do that? – ataravati Mar 05 '18 at 21:28
  • That is implementation dependent. You could: 1) avoid loading routes when you are running tests through environment variables or flags, 2) mock out your auth checking so that when you are running tests, you always appear to be logged in (using jasmine spies), 3) deregister your routes before tests run. I would prefer #2, but not knowing your implementation, other options may make more sense. – Andrew Eisenberg Mar 06 '18 at 08:04