Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Default route gets resolved in test code when calling $httpBackend.flush() or scope.$apply() #14337

Closed
achydenius opened this issue Mar 29, 2016 · 2 comments

Comments

@achydenius
Copy link

Do you want to request a feature or report a bug?

Report a breaking change (or a bug).

What is the current behavior?

When upgrading from 1.5.0 to 1.5.1 or newer, calling $httpBackend.flush() or scope.$apply() in test code causes the default route to resolve automatically. This can lead unexpected requests and break the tests. This seems to be due to change in ngRoute.

If the current behavior is a bug, please provide the steps to reproduce

Can be reproduced with for example AngularJS Phonecat demo by updating bower.json to use Angular 1.5.1 and running the tests.

@gkalpak
Copy link
Member

gkalpak commented Mar 29, 2016

Hm...this can be indeed inconvenient 😕
Basically, this is caused by either specifying a / route ($routeProvider.when('/', ...)) or redirecting to some "fallback" route ($routeProvider.otherwise(...)).

It is possible to work around this (in several ugly ways). The two most straight-forward ways out are to either add a dummy template to the $templateCache or add an expectation for the template URL:

beforeEach(module(function () {
  return function ($templateCache) {
    $templateCache.put('/default/route/template/url', '');
  };
}));

// or

beforeEach(module(function () {
  return function ($httpBackend) {
    $httpBackend.whenGET('/default/route/template/url').respond('');
  };
}));

FWIW, ui-router does a very similar fix as 8237482.
Maybe we should revert it on v1.5.x and leave it on v1.6 as a breaking change.

@gkalpak
Copy link
Member

gkalpak commented Apr 13, 2016

Of course, a more proper solution is not to include ngRoute (or a module that depends on it) in tests that don't need it 😃

gkalpak added a commit to gkalpak/angular.js that referenced this issue Apr 13, 2016
…usly loaded template"

This reverts commit 5e37b2a.
Eagerly loading `$route`, could break tests, because it might request the root or default route
template (something `$httpBackend` would know nothing about).

It will be re-applied for `v1.6.x`, with a breaking change notice and possibly a way to disable
the feature is tests.

Fixes angular#14337
gkalpak added a commit that referenced this issue Apr 13, 2016
…usly loaded template"

This reverts commit 5e37b2a.
Eagerly loading `$route`, could break tests, because it might request the root or default route
template (something `$httpBackend` would know nothing about).

It will be re-applied for `v1.6.x`, with a breaking change notice and possibly a way to disable
the feature is tests.

Fixes #14337
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.