0

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.

Juri
  • 1,531
  • 2
  • 20
  • 43
  • Can you show the $httpBackend code that you are using to mock the `app/auth/views/login.html` file? – TwitchBronBron Oct 31 '16 at 11:37
  • Could you also show the code for your directive? – TwitchBronBron Oct 31 '16 at 11:48
  • Where is `http://localhost/app/auth/views/login.html `being referenced in an your provided samples. How are you including `login.html` in your app? Could you share the code that includes that file? – TwitchBronBron Oct 31 '16 at 11:58
  • it is only used in the config .config(function ($stateProvider) { $stateProvider .state("login", { url: "/login", views: { root: { templateUrl: "app/auth/views/login.html", controller: "AuthController" } }, – Juri Oct 31 '16 at 12:06
  • I updated my answer. Take a look to see if that fixes your problem. – TwitchBronBron Oct 31 '16 at 12:16

1 Answers1

1

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

Community
  • 1
  • 1
TwitchBronBron
  • 2,783
  • 3
  • 21
  • 45