@@ -87,6 +87,103 @@ minifiers/obfuscators. In the future, we may provide a pre-processor which will
87
87
code and insert the `$inject` into the source code so that it can be minified/obfuscated.
88
88
89
89
90
+ ### Dependency inference and variable name shadowing
91
+
92
+ During inference, the injector considers argument names with leading and trailing underscores to be
93
+ equivivalent to the name without these underscores. For example `_fooSvc_` argument name is treated
94
+ as if it was `fooSvc`, this is useful especially in tests where variable name shadowing can cause
95
+ some friction. This is best illustrated on examples:
96
+
97
+ When testing a service, it's common to need a reference to it in every single test. This can be
98
+ done in jasmine with DI inference like this:
99
+
100
+ <pre>
101
+ describe('fooSvc', function() {
102
+ it('should do this thing', inject(function(fooSvc) {
103
+ //test fooSvc
104
+ }));
105
+
106
+ it('should do that thing', inject(function(fooSvc) {
107
+ //test fooSvc
108
+ }));
109
+
110
+ // more its
111
+ });
112
+ </pre>
113
+
114
+ ... but having to inject the service over and over gets easily tiresome.
115
+
116
+ It's likely better to rewrite these tests with a use of jasmine's `beforeEach`:
117
+
118
+ <pre>
119
+ describe('fooSvc', function() {
120
+ var fooSvc;
121
+
122
+ beforeEach(inject(function(fooSvc) {
123
+ fooSvc = fooSvc; // DOESN'T WORK! outer fooSvc is being shadowed
124
+ }));
125
+
126
+ it('should do this thing', function() {
127
+ //test fooSvc
128
+ });
129
+
130
+ it('should do that thing', function() {
131
+ //test fooSvc
132
+ });
133
+
134
+ // more its
135
+ });
136
+ </pre>
137
+
138
+ This obviously won't work because `fooSvc` variable in the describe block is being shadowed by the
139
+ `fooSvc` argument of the beforeEach function. So we have to resort to alternative solutions, like
140
+ for example use of array notation to annotate the beforeEach fn:
141
+
142
+ <pre>
143
+ describe('fooSvc', function() {
144
+ var fooSvc;
145
+
146
+ beforeEach(inject(['fooSvc', function(fooSvc_) {
147
+ fooSvc = fooSvc_;
148
+ }]));
149
+
150
+ it('should do this thing', function() {
151
+ //test fooSvc
152
+ });
153
+
154
+ it('should do that thing', function() {
155
+ //test fooSvc
156
+ });
157
+ });
158
+ </pre>
159
+
160
+
161
+ That's better, but it's still annoying, especially if you have many services to inject.
162
+
163
+ To resolve this shadowing problem, the injector considers `_fooSvc_` argument names equal to
164
+ `fooSvc`, so the test can be rewritten like this:
165
+
166
+ <pre>
167
+ describe('fooSvc', function() {
168
+ var fooSvc;
169
+
170
+ beforeEach(inject(function(_fooSvc_) {
171
+ fooSvc = _fooSvc_;
172
+ }));
173
+
174
+ it('should do this thing', function() {
175
+ //test fooSvc
176
+ });
177
+
178
+ it('should do that thing', function() {
179
+ //test fooSvc
180
+ });
181
+
182
+ // more its
183
+ });
184
+ </pre>
185
+
186
+
90
187
## Related Topics
91
188
92
189
* {@link dev_guide.services Angular Services}
0 commit comments