行动中的Ember-集成测试案例

时间:2015-03-08 18:16:48

标签: ember.js ember-cli ember-testing

在余烬控制器中

action:function(){
  a:function(){
   ....
   this.set('b',true);  
  }
}

我只想为此编写一个测试用例

test('a - function test case', function(assert) {
  var controller= this.subject();
  controller._action().a();
  assert(controller.get(b),true);
});

但这不起作用我收到了未定义的错误。

通过此测试用例的其他任何方式?

2 个答案:

答案 0 :(得分:0)

查看您的代码,我相信您正在尝试使用ember actions,如果是这样,您必须使用actions: { ... }代替action: function() { ... }

要触发操作,请使用send method

这是关于如何在ember-cli中测试动作的示例:

应用程序/控制器/索引

import Ember from 'ember';

export default Ember.Controller.extend({
  value: null,
  actions: {
    changeValue: function() {
      this.set('value', true);
    }
  }
});

测试/单元/控制器/索引test.js

import {
  moduleFor,
  test
} from 'ember-qunit';

moduleFor('controller:index', {});

test('it exists', function(assert) {
  var controller = this.subject();
  assert.ok(!controller.get('value'));
  controller.send('changeValue');
  assert.ok(controller.get('value'));
});

答案 1 :(得分:0)

这对我有用

test('it exists', function(assert) {
  var controller = this.subject();
  assert.ok(!controller.get('value'));
   Ember.run(function(){  
     controller.send('changeValue');
      assert.ok(controller.get('value'));
   });
});