@@ -1780,6 +1780,77 @@ angular.mock.$RootElementProvider = function() {
17801780 } ;
17811781} ;
17821782
1783+ /**
1784+ * @ngdoc service
1785+ * @name $controller
1786+ * @description
1787+ * A decorator for {@link ng.$controller} with additional `bindings` parameter, useful when testing
1788+ * controllers of directives that use {@link $compile#-bindtocontroller- `bindToController`}.
1789+ *
1790+ *
1791+ * ## Example
1792+ *
1793+ * ```js
1794+ *
1795+ * // Directive definition ...
1796+ *
1797+ * myMod.directive('myDirective', {
1798+ * controller: 'MyDirectiveController',
1799+ * bindToController: {
1800+ * name: '@'
1801+ * }
1802+ * });
1803+ *
1804+ *
1805+ * // Controller definition ...
1806+ *
1807+ * myMod.controller('MyDirectiveController', ['log', function($log) {
1808+ * $log.info(this.name);
1809+ * })];
1810+ *
1811+ *
1812+ * // In a test ...
1813+ *
1814+ * describe('myDirectiveController', function() {
1815+ * it('should write the bound name to the log', inject(function($controller, $log) {
1816+ * var ctrl = $controller('MyDirective', { /* no locals */ }, { name: 'Clark Kent' });
1817+ * expect(ctrl.name).toEqual('Clark Kent');
1818+ * expect($log.info.logs).toEqual(['Clark Kent']);
1819+ * });
1820+ * });
1821+ *
1822+ * ```
1823+ *
1824+ * @param {Function|string } constructor If called with a function then it's considered to be the
1825+ * controller constructor function. Otherwise it's considered to be a string which is used
1826+ * to retrieve the controller constructor using the following steps:
1827+ *
1828+ * * check if a controller with given name is registered via `$controllerProvider`
1829+ * * check if evaluating the string on the current scope returns a constructor
1830+ * * if $controllerProvider#allowGlobals, check `window[constructor]` on the global
1831+ * `window` object (not recommended)
1832+ *
1833+ * The string can use the `controller as property` syntax, where the controller instance is published
1834+ * as the specified property on the `scope`; the `scope` must be injected into `locals` param for this
1835+ * to work correctly.
1836+ *
1837+ * @param {Object } locals Injection locals for Controller.
1838+ * @param {Object= } bindings Properties to add to the controller before invoking the constructor. This is used
1839+ * to simulate the `bindToController` feature and simplify certain kinds of tests.
1840+ * @return {Object } Instance of given controller.
1841+ */
1842+ angular . mock . $ControllerDecorator = [ '$delegate' , function ( $delegate ) {
1843+ return function ( expression , locals , later , ident ) {
1844+ if ( later && typeof later === 'object' ) {
1845+ var create = $delegate ( expression , locals , true , ident ) ;
1846+ angular . extend ( create . instance , later ) ;
1847+ return create ( ) ;
1848+ }
1849+ return $delegate ( expression , locals , later , ident ) ;
1850+ } ;
1851+ } ] ;
1852+
1853+
17831854/**
17841855 * @ngdoc module
17851856 * @name ngMock
@@ -1808,6 +1879,7 @@ angular.module('ngMock', ['ng']).provider({
18081879 $provide . decorator ( '$$rAF' , angular . mock . $RAFDecorator ) ;
18091880 $provide . decorator ( '$$asyncCallback' , angular . mock . $AsyncCallbackDecorator ) ;
18101881 $provide . decorator ( '$rootScope' , angular . mock . $RootScopeDecorator ) ;
1882+ $provide . decorator ( '$controller' , angular . mock . $ControllerDecorator ) ;
18111883} ] ) ;
18121884
18131885/**
0 commit comments