Joe Dreimann
Joe Dreimann

Reputation: 279

Why do Angular unit tests load $route?

After updating to Angular 1.5.3 (from 1.4.9) all my unit tests have started failing, where they worked before.

The error is as follows:

Error: [$injector:unpr] Unknown provider: 
       AuthenticationHttpInterceptorProvider
         <- AuthenticationHttpInterceptor
           <- $http
             <- $templateRequest
               <- $route

It is expected that the AuthenticationHttpInterceptorProvider is not known at this point, because it is part of a different module which is not unit tested here. If I provide a mock for this interceptor, I get the error that $httpBackend wasn't expecting a GET request for the defined default route.

My question is: Why does $route get loaded in the first place, and how can I prevent it? I'm not unit testing any routes and make no changes to $route after the .config stage in the app where routes are defined. I would expect to never receive any requests for the templateUrls of the views of routes.

I've not been able to find anything in the Angular Changelog that would make me expect a different behaviour with the newer version.

Upvotes: 0

Views: 54

Answers (1)

Estus Flask
Estus Flask

Reputation: 222865

$route service is forced to be instantiated by ngRoute since 1.5.1:

Ensure $route will be instantiated in time to capture the initial $locationChangeSuccess event. This is necessary in case ngView is ncluded in an asynchronously loaded template.

This is the fix for the issue.

This means that ngRoute should be a dependency of app routing module, not other tested modules.

Upvotes: 1

Related Questions