我在对使用ic-ajax提交表单的Ember CLI组件进行验收测试时遇到了问题。
问题是,验收测试中的andThen函数并不等待表单提交得到解决。
组件:
import Ember from 'ember';
import ajax from 'ic-ajax';
export default Ember.Component.extend({
tagName: 'form',
// ...snip...
submit(e) {
e.preventDefault();
const request = ajax({
url: "url.com"
});
// Debugging in test mode, Ember.Test.lastPromise === null here?
}
// ...snip...
});验收测试:
test('it surfaces errors', function(assert) {
visit('/test');
let component;
andThen(function() {
component = find('.my-form');
fillIn('input[Placeholder="Email"]', 'not@valid');
click('button[type="submit"]');
andThen(function() {
// PROBLEM: This code doesn't wait for the ic-ajax promise to resolve
assert.ok(component.hasClass('error'), "The component applied the error classname.");
});
});
});创建ic-ajax promise似乎没有设置Ember.Test.lastPromise,这意味着andThen不会等待表单提交返回。
我是不是漏掉了什么?
发布于 2015-09-21 17:58:41
我相信这是因为组件中的提交操作执行了对服务器的异步请求。基本上,它在另一个“线程”中运行,与主程序流并行。尝试将request object promise从sumbit操作中return出来,以便andThen帮助器可以捕获和处理它。
如果这不能解决问题,您还可以使此ajax请求在测试环境中同步运行。
https://stackoverflow.com/questions/32682159
复制相似问题