AngularJS: Mock object embedded in the controller through the router

I am trying to test a controller that gets the router permission of an object:

app.js :

 ... .when('/confirm', { templateUrl: 'views/confirm.html', controller: 'ConfirmCtrl', resolve: { order: function(Order) { return Order.current; } } }) ... 

ConfirmCtrl.js :

 angular.module('angularGeolocationApp').controller('ConfirmCtrl', function($scope, order, ...) { $scope.order = order ... }); 

My test is as follows:

 'use strict'; describe('Controller: ConfirmCtrl', function () { // load the controller module beforeEach(module('angularGeolocationApp')); var ConfirmCtrl, scope; // Initialize the controller and a mock scope beforeEach(inject(function ($controller, $rootScope) { scope = $rootScope.$new(); ConfirmCtrl = $controller('ConfirmCtrl', { $scope: scope }); })); it('should get an order object', function () { expect(_.isObject(scope.order)).toBeTruthy(); }); ... }); 

However, the first wait fails:

 PhantomJS 1.9.2 (Mac OS X) Controller: ConfirmCtrl should get an order object FAILED TypeError: Attempted to assign to readonly property. at workFn (/Users/jviotti/Projects/Temporal/angular/angular-geolocation/app/bower_components/angular-mocks/angular-mocks.js:2107) Expected false to be truthy. 

My assumption is that when I test an isolated controller, the router has no changes to run the enable function and assign the correct value.

Is there any way to make fun of this order dependency?

I know that I can get rid of permission files and insert order and create an instance of $scope.order before Order.current in the controller itself, but I would like to support the resolve approach.

+7
angularjs unit-testing karma-runner
source share
1 answer

Just put your own order in a ctrl constructor like this.

 describe('Controller: ConfirmCtrl', function () { // load the controller module beforeEach(module('angularGeolocationApp')); var ConfirmCtrl, scope, order // Initialize the controller and a mock scope beforeEach(inject(function ($controller, $rootScope) { order = {}; scope = $rootScope.$new(); ConfirmCtrl = $controller('ConfirmCtrl', { $scope: scope, order: order }); })); it('should get an order object', function () { expect(_.isObject(scope.order)).toBeTruthy(); }); ... }); 

considers

+13
source share

All Articles