I have a control test that depends on the Angular $ routeParams service:
var $routeParams, MainCtrl, scope; beforeEach(inject(function ($controller, $rootScope, $injector, $templateCache) { scope = $rootScope.$new(); $routeParams = $injector.get('$routeParamsMock'); MainCtrl = $controller('MainCtrl', { $scope: scope, $routeParams: $routeParams, }); })); it('should load a pg from $routeParams', function(){ scope.userData = {}; $routeParams._setPg('PG_FIRST'); scope.$digest(); timeout.flush(); expect(scope.userData.pg).toBe(0); $routeParams._setPg('PG_SECOND'); scope.$digest(); timeout.flush(); expect(scope.userData.pg).toBe(1); });
$ routeParamsMock:
!(function(window, angular){ 'use strict'; angular.module('vitaApp') .service('$routeParamsMock', function() { var _pg = null; return{ pg: _pg, _setPg: function(pg){ _pg = pg; } } }); })(window, window.angular);
When debugging the test, I was surprised to learn that $ routeParamsMock.pg returned null each time, although I called _setPg with a different value.
This is because null is considered primitive (with the type of the object ...) and thus passed in the value ?, or, possibly, because Angular copies the object that is passed to the $ controller service.
The solution I'm looking for is preferably one that does not require the creation of different controllers in different test scenarios. eg:
MainCtrl = $controller('MainCtrl', { $scope: scope, $routeParams: {'pg': 'PG_FIRST'}, }); MainCtrl = $controller('MainCtrl', { $scope: scope, $routeParams: {'pg': 'PG_SECOND'}, });