0

在 ember 控制器中

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

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

还有其他方法可以通过这个测试用例吗?

4

2 回答 2

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'));
   });
});
于 2015-03-09T18:22:41.117 回答
0

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

并使用send 方法触发一个动作。

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

应用程序/控制器/索引

import Ember from 'ember';

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

测试/单元/控制器/index-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'));
});
于 2015-03-09T02:41:08.210 回答