angularjsunit-testingjenkinshttpbackendangular-mock

Jasmine test error on Jenkins Build Server with angular-mocks?


I'm unit testing a angular directive with Angular and Jasmine. Mocking the http backend works fine and all tests working fine locally. But on the build server i get:

Error: Unexpected request: GET app/auth/views/login.html No more request expected (line 1419) $httpBackend@bower_components/angular-mocks/angular-mocks.js:1419:90 n@build/vendor.js:222:54 build/vendor.js:219:263 build/vendor.js:254:21 $eval@build/vendor.js:268:347 $digest@build/vendor.js:265:425

My test setup:

beforeEach(angular.mock.module("app"));

beforeEach(() => {
    inject(function ($injector, _$compile_, _$rootScope_) {
      // The injector unwraps the underscores (_) from around the parameter names when matching
      $compile = _$compile_;
      $rootScope = _$rootScope_;
      $httpBackend = $injector.get("$httpBackend");
    });

$httpBackend.whenGET("api/langs/gb.json").respond({ "COMMON.HOME": homeName });
$httpBackend.whenGET("api/langs/de.json").respond({});

$httpBackend.whenGET("app/home/views/dashboard.html").respond(200, "");
$httpBackend.whenGET("app/home/views/login.html").respond(200, "");
$httpBackend.whenGET(/^private\/auth\?.*/).respond({});

directiveElem = getCompiledElement();
  });

What is different on the build server. I can't explain this behavior.


Solution

  • UI-Router is attempting to load the app/auth/views/login.html file during your app startup.

    If you run the Jasmine tests locally, you already have a web server setup at a url like http://localhost, so the request for http://localhost/app/auth/views/login.html will return the actual file. When you run this test on the build server, the build server is not configured to serve the http://localhost/app/auth/views/login.html url, so it returns a 404.

    Here is an article describing how to work around that issue: UI-router interfers with $httpbackend unit test, angular js

    Also, here's a github issue that goes into more detail about how to deal with this: https://github.com/angular-ui/ui-router/issues/212