|
1 | 1 | _Unit testing methods and computed properties follows previous patterns shown
|
2 | 2 | in [Unit Testing Basics] because Ember.Controller extends Ember.Object._
|
3 | 3 |
|
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. |
6 | 7 |
|
7 | 8 | ### Testing Controller Actions
|
8 | 9 |
|
@@ -33,43 +34,49 @@ export default Controller.extend({
|
33 | 34 | ```
|
34 | 35 |
|
35 | 36 | 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 |
37 | 38 | container:
|
38 | 39 |
|
39 | 40 | ```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'; |
41 | 43 |
|
42 |
| -moduleFor('controller:posts', {}); |
| 44 | +module('Unit | Controller | posts', function(hooks) { |
| 45 | + setupTest(hooks); |
| 46 | +}); |
43 | 47 | ```
|
44 | 48 |
|
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`. |
49 | 53 |
|
50 | 54 | ```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'; |
52 | 57 |
|
53 |
| -moduleFor('controller:posts', {}); |
| 58 | +module('Unit | Controller | posts', function(hooks) { |
| 59 | + setupTest(hooks); |
54 | 60 |
|
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); |
57 | 63 |
|
58 |
| - // get the controller instance |
59 |
| - const ctrl = this.subject(); |
| 64 | + // get the controller instance |
| 65 | + let controller = this.owner.lookup('controller:posts'); |
60 | 66 |
|
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'); |
64 | 70 |
|
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!'); |
68 | 74 |
|
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 | + }); |
73 | 80 | });
|
74 | 81 | ```
|
75 | 82 |
|
@@ -101,52 +108,40 @@ export default Controller.extend({
|
101 | 108 | });
|
102 | 109 | ```
|
103 | 110 |
|
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 |
| - |
117 | 111 | Now let's write a test that sets a property on our `post` model in the
|
118 | 112 | `PostController` that would be available on the `CommentsController`.
|
119 | 113 |
|
120 | 114 | ```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"; |
123 | 118 | import { run } from '@ember/runloop';
|
124 | 119 |
|
125 |
| -moduleFor('controller:comments', 'Comments Controller', { |
126 |
| - needs: ['controller:post'] |
127 |
| -}); |
| 120 | +module('Unit | Controller | comments', function(hooks) { |
| 121 | + setupTest(hooks); |
128 | 122 |
|
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); |
131 | 125 |
|
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'); |
135 | 129 |
|
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() { |
138 | 132 |
|
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' })); |
141 | 135 |
|
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'); |
144 | 138 |
|
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'); |
147 | 141 |
|
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 | + }); |
150 | 145 | });
|
151 | 146 | });
|
152 | 147 | ```
|
|
0 commit comments