从验收测试中触发输入操作?
Trigger an input's action from an acceptance test?
我的模板中有这个:
{{ input
value=model.title
focus-out="finishEditingTitle"
insert-newline="finishEditingTitle"
}}
动作是异步的。我想测试在用户完成编辑文本字段后,执行操作并将结果反映在页面上。
我试过了
fillIn('input', 'asdf');
keyEvent('input', 'keypress', 13);
但是不会触发动作!
同时,在 <button {{action "save"}}>
上执行 click('button');
会触发该动作。
如何通过验收测试触发输入操作?
A fiddle: http://emberjs.jsbin.com/dapuge/1/edit?html,js,output
您的输入不需要action='save'
,如果您删除它,您也不必等待点击事件。你已经在那里有一个绑定,利用它。
{{input value=model.bar}}
App.IndexController = Ember.Controller.extend({
// trigger whenever the input's value changes
_save: function() {
Ember.Logger.debug('IndexController: save');
this.get('model').save();
}.observes('model.bar'),
actions: {
// trigger the action from other DOM elements
save: function() {
this._save();
}
}
});
我编辑了你的例子,现在所有测试都通过了:
http://emberjs.jsbin.com/baqoguyapa/1/edit?html,js,output
因此,您希望等待 UI 事件完成后再进行测试。
你需要继续履行承诺。如果您的需求更复杂,您将不得不在控制器中编写自定义代码以确保发生这种情况。 (例如:Using Ember (cli) how do I get an acceptance test to wait for a promise?)。
幸运的是,对于您的情况,已经有处理异步的助手,请参阅:http://emberjs.com/guides/testing/test-helpers/
实际答案,只是:
click('.title');
fillIn('input', 'asdf');
triggerEvent('input', 'blur');
注意,这行不通:
//triggerEvent('input', 'blur');
Ember.$('<input>').blur();
定义如下:
http://snipplr.com/view/53641/jquery-simulate-keypress-trigger/
使用 keyup 而不是 keypress:
fillIn('input', 'asdf');
keyEvent('input', 'keyup', 13);
我的模板中有这个:
{{ input
value=model.title
focus-out="finishEditingTitle"
insert-newline="finishEditingTitle"
}}
动作是异步的。我想测试在用户完成编辑文本字段后,执行操作并将结果反映在页面上。
我试过了
fillIn('input', 'asdf');
keyEvent('input', 'keypress', 13);
但是不会触发动作!
同时,在 <button {{action "save"}}>
上执行 click('button');
会触发该动作。
如何通过验收测试触发输入操作?
A fiddle: http://emberjs.jsbin.com/dapuge/1/edit?html,js,output
您的输入不需要action='save'
,如果您删除它,您也不必等待点击事件。你已经在那里有一个绑定,利用它。
{{input value=model.bar}}
App.IndexController = Ember.Controller.extend({
// trigger whenever the input's value changes
_save: function() {
Ember.Logger.debug('IndexController: save');
this.get('model').save();
}.observes('model.bar'),
actions: {
// trigger the action from other DOM elements
save: function() {
this._save();
}
}
});
我编辑了你的例子,现在所有测试都通过了: http://emberjs.jsbin.com/baqoguyapa/1/edit?html,js,output
因此,您希望等待 UI 事件完成后再进行测试。
你需要继续履行承诺。如果您的需求更复杂,您将不得不在控制器中编写自定义代码以确保发生这种情况。 (例如:Using Ember (cli) how do I get an acceptance test to wait for a promise?)。
幸运的是,对于您的情况,已经有处理异步的助手,请参阅:http://emberjs.com/guides/testing/test-helpers/
实际答案,只是:
click('.title');
fillIn('input', 'asdf');
triggerEvent('input', 'blur');
注意,这行不通:
//triggerEvent('input', 'blur');
Ember.$('<input>').blur();
定义如下: http://snipplr.com/view/53641/jquery-simulate-keypress-trigger/
使用 keyup 而不是 keypress:
fillIn('input', 'asdf');
keyEvent('input', 'keyup', 13);