mardi 3 février 2015

Angular - mock nested controllers to test a factory throwing error


I'm building a page state manager that is going to save a number of states into the URL and will tie into scope variables. I need to be able to handle nested scopes and build prefixes that will allow sibling modules/controllers to all maintain separate state in the URL.


I'm just trying to set up a unit test for this configuration but having issues. Have a test that looks like:



it('should generate correct prefixes for nested scopes', inject(function(StateManager, $rootScope, $controller){
var stateManager, stateManager2, scope, controller1, controller2, rootScope = $rootScope;

controller1 = $controller('TestCtrl', {
$scope: rootScope
});
});


But this is generating the error Error: [ng:areq] Argument 'TestCtrl' is not a function, got undefined. This is what I'm moving towards:



it('should attach a unique fullName and localName to each scope', inject(function(StateManager, $rootScope, $controller){
var stateManager, stateManager2, scope, controller1, controller2, rootScope = $rootScope;

controller1 = $controller('TestCtrl', {
$scope: rootScope
});

stateManager = new StateManager('Controller1', rootScope, {defaults: {test: true}});

scope = $rootScope.$new();
scope.$index = 1;

controller2 = $controller('ChildController', {
$scope: scope
});

stateManager2 = new StateManager('Controller2', scope, {defaults: {test: false}});

// do tests

});


But I can't even get by the first error. I obviously don't have TestCtrl or ChildController defined, but I just want some mock controllers here so I can play with scopes and child scopes. Thoughts?





Aucun commentaire:

Enregistrer un commentaire