@@ -206,19 +206,17 @@ are applied to the scope object.
206
206
207
207
## Testing Controllers
208
208
209
- The way to test a controller depends upon how complicated the controller is.
210
-
211
- - If your controller doesn't use DI or scope methods — create the controller with the `new`
212
- operator and test away. For example:
209
+ Although there are many ways to test a controller, one of the best conventions, shown below,
210
+ involves injecting the `$rootScope` and `$controller`
213
211
214
212
Controller Function:
215
213
<pre>
216
- function myController() {
217
- this .spices = [{"name":"pasilla", "spiciness":"mild"},
214
+ function myController($scope ) {
215
+ $scope .spices = [{"name":"pasilla", "spiciness":"mild"},
218
216
{"name":"jalapeno", "spiceiness":"hot hot hot!"},
219
217
{"name":"habanero", "spiceness":"LAVA HOT!!"}];
220
218
221
- this .spice = "habanero";
219
+ $scope .spice = "habanero";
222
220
}
223
221
</pre>
224
222
@@ -227,27 +225,51 @@ Controller Test:
227
225
describe('myController function', function() {
228
226
229
227
describe('myController', function() {
230
- var ctrl ;
228
+ var scope ;
231
229
232
- beforeEach(function() {
233
- ctrl = new myController();
234
- });
230
+ beforeEach(inject(function($rootScope, $controller) {
231
+ scope = $rootScope.$new();
232
+ var ctrl = $controller(myController, {$scope: scope});
233
+ }));
235
234
236
235
it('should create "spices" model with 3 spices', function() {
237
- expect(ctrl .spices.length).toBe(3);
236
+ expect(scope .spices.length).toBe(3);
238
237
});
239
238
240
239
it('should set the default value of spice', function() {
241
- expect(ctrl .spice).toBe('habanero');
240
+ expect(scope .spice).toBe('habanero');
242
241
});
243
242
});
244
243
});
245
244
</pre>
246
245
247
- - If your controller does use DI or scope methods — create a root scope, then create the controller
248
- in the root scope with `scope.$new(MyController)`. Test the controller using `$eval`, if necessary.
249
- - If you need to test a nested controller that depends on its parent's state — create a root scope,
250
- create a parent scope, create a child scope, and test the controller using $eval if necessary.
246
+
247
+ If you need to test a nested controller one needs to create the same scope hierarchy
248
+ in your test as exist in the DOM.
249
+
250
+ <pre>
251
+ describe('state', function() {
252
+ var mainScope, childScope, babyScope;
253
+
254
+ beforeEach(inject(function($rootScope, $controller) {
255
+ mainScope = $rootScope.$new();
256
+ var mainCtrl = $controller(MainCtrl, {$scope: mainScope});
257
+ childScope = mainScope.$new();
258
+ var childCtrl = $controller(ChildCtrl, {$scope: childScope});
259
+ babyScope = $rootScope.$new();
260
+ var babyCtrl = $controller(BabyCtrl, {$scope: babyScope});
261
+ }));
262
+
263
+ it('should have over and selected', function() {
264
+ expect(mainScope.timeOfDay).toBe('morning');
265
+ expect(mainScope.name).toBe('Nikki');
266
+ expect(childScope.timeOfDay).toBe('morning');
267
+ expect(childScope.name).toBe('Mattie');
268
+ expect(babyScope.timeOfDay).toBe('evening');
269
+ expect(babyScope.name).toBe('Gingerbreak Baby');
270
+ });
271
+ });
272
+ </pre>
251
273
252
274
253
275
## Related Topics
0 commit comments