【问题标题】:Ember CLI Testing with ic-ajax使用 ic-ajax 进行 Ember CLI 测试
【发布时间】:2015-12-17 09:10:19
【问题描述】:

我在接受测试使用 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 承诺似乎没有设置Ember.Test.lastPromise,这意味着andThen 不会等待表单提交返回。

我错过了什么吗?

【问题讨论】:

    标签: javascript ajax ember.js


    【解决方案1】:

    我相信这是因为组件中的提交操作向服务器执行了异步请求。基本上它在另一个“线程”中运行,与主程序流并行。所以andThen helper 对此一无所知。尝试从sumbit 操作中对return 请求对象承诺,以便andThen 助手可以捕获并处理它。

    如果这没有帮助,您还可以让这个 ajax 请求在测试环境中同步运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-06
      • 2014-11-03
      • 2017-11-07
      相关资源
      最近更新 更多