A decorator for $controller with additional bindings parameter, useful when testing
controllers of directives that use bindToController.
Depending on the value of
preAssignBindingsEnabled(), the properties
will be bound before or after invoking the constructor.
// Directive definition ...
myMod.directive('myDirective', {
controller: 'MyDirectiveController',
bindToController: {
name: '@'
}
});
// Controller definition ...
myMod.controller('MyDirectiveController', ['$log', function($log) {
this.log = function() {
$log.info(this.name);
};
}]);
// In a test ...
describe('myDirectiveController', function() {
describe('log()', function() {
it('should write the bound name to the log', inject(function($controller, $log) {
var ctrl = $controller('MyDirectiveController', { /* no locals */ }, { name: 'Clark Kent' });
ctrl.log();
expect(ctrl.name).toEqual('Clark Kent');
expect($log.info.logs).toEqual(['Clark Kent']);
}));
});
});
$controller(constructor, locals, [bindings]);
| Param | Type | Details |
|---|---|---|
| constructor | function()string |
If called with a function then it's considered to be the controller constructor function. Otherwise it's considered to be a string which is used to retrieve the controller constructor using the following steps:
|
| locals | Object |
Injection locals for Controller. |
|
bindings
(optional)
|
Object |
Properties to add to the controller instance. This is used to simulate
the |
| Object | Instance of given controller. |