Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit e12e584

Browse files
jasonm23petebacondarwin
authored andcommitted
docs(mock.inject): document underscore wrapping syntax
Add a summary describing the ignored underscore syntax sugar helper, with a simple use case example. Closes #3621
1 parent 42a5f1e commit e12e584

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

src/ngMock/angular-mocks.js

+34-2
Original file line numberDiff line numberDiff line change
@@ -1892,8 +1892,40 @@ angular.mock.clearDataCache = function() {
18921892
* instance of {@link AUTO.$injector $injector} per test, which is then used for
18931893
* resolving references.
18941894
*
1895-
* See also {@link angular.mock.module module}
18961895
*
1896+
* ## Resolving References (Underscore Wrapping)
1897+
* Often, we would like to inject a reference once, in a `beforeEach()` block and reuse this
1898+
* in multiple `it()` clauses. To be able to do this we must assign the reference to a variable
1899+
* that is declared in the scope of the `describe()` block. Since we would, most likely, want
1900+
* the variable to have the same name of the reference we have a problem, since the parameter
1901+
* to the `inject()` function would hide the outer variable.
1902+
*
1903+
* To help with this, the injected parameters can, optionally, beenclosing with underscores.
1904+
* These are ignored by the injector when the reference name is resolved.
1905+
*
1906+
* For example, the parameter `_myService_` would be resolved as the reference `myService`.
1907+
* Since it is available in the function body as _myService_, we can then assign it to a variable
1908+
* defined in an outer scope.
1909+
*
1910+
* ```
1911+
* // Defined out reference variable outside
1912+
* var myService;
1913+
*
1914+
* // Wrap the parameter in underscores
1915+
* beforeEach( inject( function(_myService_){
1916+
* myService = _myService_;
1917+
* }));
1918+
*
1919+
* // Use myService in a series of tests.
1920+
* it('makes use of myService', function() {
1921+
* myService.doStuff();
1922+
* });
1923+
*
1924+
* ```
1925+
*
1926+
* See also {@link angular.mock.module angular.mock.module}
1927+
*
1928+
* ## Example
18971929
* Example of what a typical jasmine tests looks like with the inject method.
18981930
* <pre>
18991931
*
@@ -1930,7 +1962,7 @@ angular.mock.clearDataCache = function() {
19301962
* });
19311963
*
19321964
* </pre>
1933-
*
1965+
*
19341966
* @param {...Function} fns any number of functions which will be injected using the injector.
19351967
*/
19361968
window.inject = angular.mock.inject = function() {

0 commit comments

Comments
 (0)