如何对导致 Action 的方法进行单元测试
How can I unit test a method that results in an Action
在我的 Ember 应用程序中,我有一个简单的表单组件,具有以下行为(片段):
...
let searchText = this.get('searchText') &&
this.get('searchText').trim().toLowerCase();
this.sendAction('searchTextChanged', searchText);
...
我如何 unit 在 qunit 测试中测试此功能?我不知道如何监听操作结果。我正在使用 ember-cli 附带的测试功能 -- qunit、助手等
Ember 有一个关于如何做到这一点的指南 here。我会把代码放在这里以防指南发生变化。
鉴于此组件:
App.MyFooComponent = Ember.Component.extend({
layout:Ember.Handlebars.compile("<button {{action 'doSomething'}}></button>"),
actions: {
doSomething: function() {
this.sendAction('internalAction');
}
}
});
您将像这样测试操作:
moduleForComponent('my-foo', 'MyFooComponent');
test('trigger external action when button is clicked', function() {
// tell our test to expect 1 assertion
expect(1);
// component instance
var component = this.subject();
// component dom instance
var $component = this.append();
var targetObject = {
externalAction: function() {
// we have the assertion here which will be
// called when the action is triggered
ok(true, 'external Action was called!');
}
};
// setup a fake external action to be called when
// button is clicked
component.set('internalAction', 'externalAction');
// set the targetObject to our dummy object (this
// is where sendAction will send its action to)
component.set('targetObject', targetObject);
// click the button
click('button');
});
在我的 Ember 应用程序中,我有一个简单的表单组件,具有以下行为(片段):
...
let searchText = this.get('searchText') &&
this.get('searchText').trim().toLowerCase();
this.sendAction('searchTextChanged', searchText);
...
我如何 unit 在 qunit 测试中测试此功能?我不知道如何监听操作结果。我正在使用 ember-cli 附带的测试功能 -- qunit、助手等
Ember 有一个关于如何做到这一点的指南 here。我会把代码放在这里以防指南发生变化。
鉴于此组件:
App.MyFooComponent = Ember.Component.extend({
layout:Ember.Handlebars.compile("<button {{action 'doSomething'}}></button>"),
actions: {
doSomething: function() {
this.sendAction('internalAction');
}
}
});
您将像这样测试操作:
moduleForComponent('my-foo', 'MyFooComponent');
test('trigger external action when button is clicked', function() {
// tell our test to expect 1 assertion
expect(1);
// component instance
var component = this.subject();
// component dom instance
var $component = this.append();
var targetObject = {
externalAction: function() {
// we have the assertion here which will be
// called when the action is triggered
ok(true, 'external Action was called!');
}
};
// setup a fake external action to be called when
// button is clicked
component.set('internalAction', 'externalAction');
// set the targetObject to our dummy object (this
// is where sendAction will send its action to)
component.set('targetObject', targetObject);
// click the button
click('button');
});