-
Notifications
You must be signed in to change notification settings - Fork 3k
Closed
Description
Currently, changes to the $stateParams object in one test are visible in another. Example Jasmine test suite to illustrate the issue:
describe('stateparams', function () {
var inject = angular.mock.inject;
var module = angular.mock.module;
beforeEach(module('ui.router'));
it('should be empty', inject(function ($stateParams) {
expect($stateParams.foo).toBeUndefined(); // WORKS
$stateParams.foo = 'bar';
}));
it('should be cleared between tests', inject(function ($stateParams) {
expect($stateParams.foo).toBeUndefined(); // FAILS
}));
});
Angular normally avoids problems like this one by re-initializing modules before every test case. However, in the case of $stateParams, that does nothing. The $stateParams service is defined as a value(), and will return the same object instance every time it's "initialized".
angular.module('ui.router.state')
.value('$stateParams', {});
// ...should be...
angular.module('ui.router.state')
.factory('$stateParams', function () { return {}; });
Defining it as a factory() would solve the issue. Since Angular services are singletons, initialization would still only happen once when running your application normally. Test cases would no longer be forced to manually reset this object, however.