Skip to content

Commit 0dc9457

Browse files
committedMar 4, 2015
feat(ngMock): allow mock $controller service to set up controller bindings
Adds a new mock for the $controller service, in order to simplify testing using the bindToController feature. ```js var dictionaryOfControllerBindings = { data: [ { id: 0, phone: '...', name: '...' }, { id: 1, phone: '...', name: '...' }, ] }; // When the MyCtrl constructor is called, `this.data ~= dictionaryOfControllerBindings.data` $controller(MyCtrl, myLocals, dictionaryOfControllerBindings); ``` Closes angular#9425
1 parent 4374f89 commit 0dc9457

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
 

‎src/ngMock/angular-mocks.js

+39
Original file line numberDiff line numberDiff line change
@@ -1780,6 +1780,44 @@ angular.mock.$RootElementProvider = function() {
17801780
};
17811781
};
17821782

1783+
/**
1784+
* @ngdoc service
1785+
* @name $controller
1786+
* @description
1787+
* Mock $controller service, used to simplify testing controllers when using the
1788+
* bindToController feature.
1789+
*
1790+
* @param {Function|string} constructor If called with a function then it's considered to be the
1791+
* controller constructor function. Otherwise it's considered to be a string which is used
1792+
* to retrieve the controller constructor using the following steps:
1793+
*
1794+
* * check if a controller with given name is registered via `$controllerProvider`
1795+
* * check if evaluating the string on the current scope returns a constructor
1796+
* * if $controllerProvider#allowGlobals, check `window[constructor]` on the global
1797+
* `window` object (not recommended)
1798+
*
1799+
* The string can use the `controller as property` syntax, where the controller instance is published
1800+
* as the specified property on the `scope`; the `scope` must be injected into `locals` param for this
1801+
* to work correctly.
1802+
*
1803+
* @param {Object} locals Injection locals for Controller.
1804+
* @param {Object=} bindings Properties to add to the controller before invoking
1805+
* the constructor. This is used to simulate the `bindToController` feature
1806+
* and simplify certain kinds oftests.
1807+
* @return {Object} Instance of given controller.
1808+
*/
1809+
angular.mock.$ControllerDecorator = ['$delegate', function($delegate) {
1810+
return function(expression, locals, later, ident) {
1811+
if (later && typeof later === 'object') {
1812+
var create = $delegate(expression, locals, true, ident);
1813+
angular.extend(create.instance, later);
1814+
return create();
1815+
}
1816+
return $delegate(expression, locals, later, ident);
1817+
};
1818+
}];
1819+
1820+
17831821
/**
17841822
* @ngdoc module
17851823
* @name ngMock
@@ -1808,6 +1846,7 @@ angular.module('ngMock', ['ng']).provider({
18081846
$provide.decorator('$$rAF', angular.mock.$RAFDecorator);
18091847
$provide.decorator('$$asyncCallback', angular.mock.$AsyncCallbackDecorator);
18101848
$provide.decorator('$rootScope', angular.mock.$RootScopeDecorator);
1849+
$provide.decorator('$controller', angular.mock.$ControllerDecorator);
18111850
}]);
18121851

18131852
/**

‎test/ngMock/angular-mocksSpec.js

+22
Original file line numberDiff line numberDiff line change
@@ -1751,6 +1751,28 @@ describe('ngMock', function() {
17511751
}));
17521752
});
17531753
});
1754+
1755+
1756+
describe('$controllerDecorator', function() {
1757+
it('should support creating controller with bindings', function() {
1758+
var called = false;
1759+
var data = [
1760+
{ name: 'derp1', id: 0 },
1761+
{ name: 'testname', id: 1 },
1762+
{ name: 'flurp', id: 2 }
1763+
];
1764+
module(function($controllerProvider) {
1765+
$controllerProvider.register('testCtrl', function() {
1766+
called = true;
1767+
expect(this.data).toEqual(data);
1768+
});
1769+
});
1770+
inject(function($controller, $rootScope) {
1771+
$controller('testCtrl', { scope: $rootScope }, { data: data });
1772+
expect(called).toBe(true);
1773+
});
1774+
});
1775+
});
17541776
});
17551777

17561778

0 commit comments

Comments
 (0)
Please sign in to comment.