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

Commit 5b1f9b3

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 08a07f2 commit 5b1f9b3

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
@@ -1735,8 +1735,40 @@ window.jasmine && (function(window) {
17351735
* instance of {@link AUTO.$injector $injector} per test, which is then used for
17361736
* resolving references.
17371737
*
1738-
* See also {@link angular.mock.module module}
17391738
*
1739+
* ## Resolving References (Underscore Wrapping)
1740+
* Often, we would like to inject a reference once, in a `beforeEach()` block and reuse this
1741+
* in multiple `it()` clauses. To be able to do this we must assign the reference to a variable
1742+
* that is declared in the scope of the `describe()` block. Since we would, most likely, want
1743+
* the variable to have the same name of the reference we have a problem, since the parameter
1744+
* to the `inject()` function would hide the outer variable.
1745+
*
1746+
* To help with this, the injected parameters can, optionally, beenclosing with underscores.
1747+
* These are ignored by the injector when the reference name is resolved.
1748+
*
1749+
* For example, the parameter `_myService_` would be resolved as the reference `myService`.
1750+
* Since it is available in the function body as _myService_, we can then assign it to a variable
1751+
* defined in an outer scope.
1752+
*
1753+
* ```
1754+
* // Defined out reference variable outside
1755+
* var myService;
1756+
*
1757+
* // Wrap the parameter in underscores
1758+
* beforeEach( inject( function(_myService_){
1759+
* myService = _myService_;
1760+
* }));
1761+
*
1762+
* // Use myService in a series of tests.
1763+
* it('makes use of myService', function() {
1764+
* myService.doStuff();
1765+
* });
1766+
*
1767+
* ```
1768+
*
1769+
* See also {@link angular.mock.module angular.mock.module}
1770+
*
1771+
* ## Example
17401772
* Example of what a typical jasmine tests looks like with the inject method.
17411773
* <pre>
17421774
*
@@ -1773,7 +1805,7 @@ window.jasmine && (function(window) {
17731805
* });
17741806
*
17751807
* </pre>
1776-
*
1808+
*
17771809
* @param {...Function} fns any number of functions which will be injected using the injector.
17781810
*/
17791811
window.inject = angular.mock.inject = function() {

0 commit comments

Comments
 (0)