Unit testing methods and computed properties follows previous patterns shown in [Unit Testing Basics] because Ember.Controller extends Ember.Object.

单元测试方案和计算属性与之前单元测试基础中说明的相同,因为Ember.Controller集成自Ember.Object

Unit testing controllers is very simple using the unit test helper moduleFor which is part of the ember-qunit framework.

针对控制器的单元测试使用ember-qunit框架的moduleFor来做使这一切变得非常简单。

Testing Controller Actions

测试控制器操作

Here we have a controller PostsController with some computed properties and an action setProps.

下面给出一个PostsController控制器,控制器有一些计算属性和一个setProps操作。

  1. App.PostsController = Ember.ArrayController.extend({
  2. propA: 'You need to write tests',
  3. propB: 'And write one for me too',
  4. setPropB: function(str) {
  5. this.set('propB', str);
  6. },
  7. actions: {
  8. setProps: function(str) {
  9. this.set('propA', 'Testing is cool');
  10. this.setPropB(str);
  11. }
  12. }
  13. });

setProps sets a property on the controller and also calls a method. To write a test for this action, we would use the moduleFor helper to setup a test container:

setProps设置控制器的一个属性并调用一个方法。为这个操作编写测试,需要使用moduleFor助手来设置一个测试容器:

  1. moduleFor('controller:posts', 'Posts Controller');

Next we use this.subject() to get an instance of the PostsController and write a test to check the action. this.subject() is a helper method from the ember-qunit library that returns a singleton instance of the module set up using moduleFor.

接下来使用this.subject()来获取PostsController的一个实例,并编写一个测试来检测这个操作。this.subject()是ember-qunit库提供的一个助手方法,其返回moduleFor设置的模块的一个单例。

  1. test('calling the action setProps updates props A and B', function() {
  2. expect(4);
  3. // get the controller instance
  4. var ctrl = this.subject();
  5. // check the properties before the action is triggered
  6. equal(ctrl.get('propA'), 'You need to write tests');
  7. equal(ctrl.get('propB'), 'And write one for me too');
  8. // trigger the action on the controller by using the `send` method,
  9. // passing in any params that our action may be expecting
  10. ctrl.send('setProps', 'Testing Rocks!');
  11. // finally we assert that our values have been updated
  12. // by triggering our action.
  13. equal(ctrl.get('propA'), 'Testing is cool');
  14. equal(ctrl.get('propB'), 'Testing Rocks!');
  15. });

Live Example

在线示例

Unit Testing Controllers “Actions”

控制器“操作”单元测试

Testing Controller Needs

测试控制器依赖

Sometimes controllers have dependencies on other controllers. This is accomplished by using needs. For example, here are two simple controllers. The PostController is a dependency of the CommentsController:

有时候控制器需要依赖其他控制器。这通过needs来实现。例如,下面有两个简单的控制器。PostControllerCommentsController的一个依赖:

  1. App.PostController = Ember.ObjectController.extend({
  2. // ...
  3. });
  4. App.CommentsController = Ember.ArrayController.extend({
  5. needs: 'post',
  6. title: Ember.computed.alias('controllers.post.title'),
  7. });

This time when we setup our moduleFor we need to pass an options object as our third argument that has the controller’s needs.

在设置moduleFor之时,需要将一个选项对象作为第三个参数,该参数是控制器的needs

  1. moduleFor('controller:comments', 'Comments Controller', {
  2. needs: ['controller:post']
  3. });

Now let’s write a test that sets a property on our post model in the PostController that would be available on the CommentsController.

下面来写一个,测试中设置PostController中得post模型的一个属性,这个属性在CommentsController同样有效。

  1. test('modify the post', function() {
  2. expect(2);
  3. // grab an instance of `CommentsController` and `PostController`
  4. var ctrl = this.subject(),
  5. postCtrl = ctrl.get('controllers.post');
  6. // wrap the test in the run loop because we are dealing with async functions
  7. Ember.run(function() {
  8. // set a generic model on the post controller
  9. postCtrl.set('model', Ember.Object.create({ title: 'foo' }));
  10. // check the values before we modify the post
  11. equal(ctrl.get('title'), 'foo');
  12. // modify the title of the post
  13. postCtrl.get('model').set('title', 'bar');
  14. // assert that the controllers title has changed
  15. equal(ctrl.get('title'), 'bar');
  16. });
  17. });

Live Example

在线示例

Unit Testing Controllers “Needs”

控制器依赖单元测试