Skip to content

Commit e7cd0bc

Browse files
Daniel ZenIgorMinar
authored andcommitted
docs(guide/controllers): add a section on testing controllers
1 parent ade6c45 commit e7cd0bc

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

docs/content/guide/dev_guide.mvc.understanding_controller.ngdoc

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -206,19 +206,17 @@ are applied to the scope object.
206206

207207
## Testing Controllers
208208

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`
213211

214212
Controller Function:
215213
<pre>
216-
function myController() {
217-
this.spices = [{"name":"pasilla", "spiciness":"mild"},
214+
function myController($scope) {
215+
$scope.spices = [{"name":"pasilla", "spiciness":"mild"},
218216
{"name":"jalapeno", "spiceiness":"hot hot hot!"},
219217
{"name":"habanero", "spiceness":"LAVA HOT!!"}];
220218

221-
this.spice = "habanero";
219+
$scope.spice = "habanero";
222220
}
223221
</pre>
224222

@@ -227,27 +225,51 @@ Controller Test:
227225
describe('myController function', function() {
228226

229227
describe('myController', function() {
230-
var ctrl;
228+
var scope;
231229

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+
}));
235234

236235
it('should create "spices" model with 3 spices', function() {
237-
expect(ctrl.spices.length).toBe(3);
236+
expect(scope.spices.length).toBe(3);
238237
});
239238

240239
it('should set the default value of spice', function() {
241-
expect(ctrl.spice).toBe('habanero');
240+
expect(scope.spice).toBe('habanero');
242241
});
243242
});
244243
});
245244
</pre>
246245

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>
251273

252274

253275
## Related Topics

0 commit comments

Comments
 (0)