Skip to content

Commit 21a72bc

Browse files
committed
update(testing): upd. controller section (emberjs/ember.js#15933)
This updates the controller testing section according to the new Ember Qunit testing patterns proposed in RFC#232.
1 parent 0e60a81 commit 21a72bc

File tree

1 file changed

+53
-58
lines changed

1 file changed

+53
-58
lines changed

source/testing/testing-controllers.md

+53-58
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
_Unit testing methods and computed properties follows previous patterns shown
22
in [Unit Testing Basics] because Ember.Controller extends Ember.Object._
33

4-
Unit testing controllers is very simple using the unit test helper which is part
5-
of the ember-qunit framework.
4+
Controllers can be tested using the `setupTest` helper which is part
5+
of the ember-qunit framework. The tests written for instances like `Ember.Controller` are
6+
also described as container tests.
67

78
### Testing Controller Actions
89

@@ -33,43 +34,49 @@ export default Controller.extend({
3334
```
3435

3536
The `setProps` action directly sets one property, and calls the method to set the other.
36-
In our generated test, ember-cli already uses the `moduleFor` helper to set up a test
37+
In our generated test file, ember-cli already uses the `module` and the `setupTest` helpers to set up a test
3738
container:
3839

3940
```tests/unit/controllers/posts-test.js
40-
import { moduleFor, test } from 'ember-qunit';
41+
import { module, test } from 'qunit';
42+
import { setupTest } from 'ember-qunit';
4143

42-
moduleFor('controller:posts', {});
44+
module('Unit | Controller | posts', function(hooks) {
45+
setupTest(hooks);
46+
});
4347
```
4448

45-
Next we use `this.subject()` to get an instance of the `PostsController` and
46-
write a test to check the action. `this.subject()` is a helper method from the
47-
`ember-qunit` library that returns a singleton instance of the module set up
48-
using `moduleFor`.
49+
Next we use the owner API to gain access to the controller we'd like to test.
50+
Using the `this.owner.lookup` method we get the instance of the `PostsController` and can check the action in our test.
51+
The `this.owner.lookup` helper returns objects generated by the framework in your applications
52+
and is also exposed in tests for your usage. Here it will return a singleton instance of the `PostsController`.
4953

5054
```tests/unit/controllers/posts-test.js
51-
import { moduleFor, test } from 'ember-qunit';
55+
import { module, test } from 'qunit';
56+
import { setupTest } from 'ember-qunit';
5257

53-
moduleFor('controller:posts', {});
58+
module('Unit | Controller | posts', function(hooks) {
59+
setupTest(hooks);
5460

55-
test('should update A and B on setProps action', function(assert) {
56-
assert.expect(4);
61+
test('should update A and B on setProps action', function(assert) {
62+
assert.expect(4);
5763

58-
// get the controller instance
59-
const ctrl = this.subject();
64+
// get the controller instance
65+
let controller = this.owner.lookup('controller:posts');
6066

61-
// check the properties before the action is triggered
62-
assert.equal(ctrl.get('propA'), 'You need to write tests', 'propA initialized');
63-
assert.equal(ctrl.get('propB'), 'And write one for me too', 'propB initialized');
67+
// check the properties before the action is triggered
68+
assert.equal(controller.get('propA'), 'You need to write tests', 'propA initialized');
69+
assert.equal(controller.get('propB'), 'And write one for me too', 'propB initialized');
6470

65-
// trigger the action on the controller by using the `send` method,
66-
// passing in any params that our action may be expecting
67-
ctrl.send('setProps', 'Testing Rocks!');
71+
// trigger the action on the controller by using the `send` method,
72+
// passing in any params that our action may be expecting
73+
controller.send('setProps', 'Testing Rocks!');
6874

69-
// finally we assert that our values have been updated
70-
// by triggering our action.
71-
assert.equal(ctrl.get('propA'), 'Testing is cool', 'propA updated');
72-
assert.equal(ctrl.get('propB'), 'Testing Rocks!', 'propB updated');
75+
// finally we assert that our values have been updated
76+
// by triggering our action.
77+
assert.equal(controller.get('propA'), 'Testing is cool', 'propA updated');
78+
assert.equal(controller.get('propB'), 'Testing Rocks!', 'propB updated');
79+
});
7380
});
7481
```
7582

@@ -101,52 +108,40 @@ export default Controller.extend({
101108
});
102109
```
103110

104-
This time when we setup our `moduleFor` we need to pass an options object as
105-
our third argument that has the controller's `needs`.
106-
107-
```tests/unit/controllers/comments-test.js
108-
import { moduleFor, test } from 'ember-qunit';
109-
import EmberObject from '@ember/object';
110-
import { run } from '@ember/runloop';
111-
112-
moduleFor('controller:comments', 'Comments Controller', {
113-
needs: ['controller:post']
114-
});
115-
```
116-
117111
Now let's write a test that sets a property on our `post` model in the
118112
`PostController` that would be available on the `CommentsController`.
119113

120114
```tests/unit/controllers/comments-test.js
121-
import { moduleFor, test } from 'ember-qunit';
122-
import EmberObject from '@ember/object';
115+
import { module, test } from 'qunit';
116+
import { setupTest } from 'ember-qunit';
117+
import EmberObject from "@ember/object";
123118
import { run } from '@ember/runloop';
124119

125-
moduleFor('controller:comments', 'Comments Controller', {
126-
needs: ['controller:post']
127-
});
120+
module('Unit | Controller | comments', function(hooks) {
121+
setupTest(hooks);
128122

129-
test('should modify the post model', function(assert) {
130-
assert.expect(2);
123+
test('should modify the post model', function(assert) {
124+
assert.expect(2);
131125

132-
// grab an instance of `CommentsController` and `PostController`
133-
const ctrl = this.subject();
134-
const postCtrl = ctrl.get('post');
126+
// grab an instance of `CommentsController` and `PostController`
127+
let controller = this.owner.lookup('controller:comments');
128+
let postCtrl = controller.get('post');
135129

136-
// wrap the test in the run loop because we are dealing with async functions
137-
run(function() {
130+
// wrap the test in the run loop because we are dealing with async functions
131+
run(function() {
138132

139-
// set a generic model on the post controller
140-
postCtrl.set('model', Ember.Object.create({ title: 'foo' }));
133+
// set a generic model on the post controller
134+
postCtrl.set('model', Ember.Object.create({ title: 'foo' }));
141135

142-
// check the values before we modify the post
143-
assert.equal(ctrl.get('title'), 'foo', 'title is set');
136+
// check the values before we modify the post
137+
assert.equal(controller.get('title'), 'foo', 'title is set');
144138

145-
// modify the title of the post
146-
postCtrl.get('model').set('title', 'bar');
139+
// modify the title of the post
140+
postCtrl.get('model').set('title', 'bar');
147141

148-
// assert that the controllers title has changed
149-
assert.equal(ctrl.get('title'), 'bar', 'title is updated');
142+
// assert that the controllers title has changed
143+
assert.equal(controller.get('title'), 'bar', 'title is updated');
144+
});
150145
});
151146
});
152147
```

0 commit comments

Comments
 (0)