3

I am writing an integration test for my Ember.js application in QUnit. Before a test, I want to seed some test data by issuing HTTP requests to a dedicated testing API. I use jQuery.post to issue POST requests and I use Ember.RSVP.Promise.cast to turn the jQuery promise into an RSVP promise. However, it never seems to resolve. In the code below, it just hangs. The string "STARTING" is printed but neither "DONE" nor "FAIL" is printed.

I also tried creating a new RSVP Promise as described in the "Advanced usage" section of http://emberjs.com/api/classes/Ember.RSVP.Promise.html, to no avail (it also hanged). If I don't wrap the jQuery promise into an RSVP Promise, it does reach either the "DONE" or "FAIL".

Why doesn't the RSVP Promise resolve?

function create_teacher() {
  var url = "<%= testing_teacher_path %>";

  return Ember.RSVP.Promise.cast(
    Ember.$.post(
      url,
      {
        user: {
          first_name: "John",
          last_name: "Doe"
          school: "EE3",
          email: "john@doe.com",
          password: "password"
        }
      }
    )
  );
}

module("Teacher Dashboard", {
  setup: function() {
    console.log("STARTING");
    Ember.run(HstryEd, HstryEd.advanceReadiness);
  },
  teardown: function() {
    console.log("TEARING DOWN");
    HstryEd.reset();
  }
});

asyncTest("Login", function() {
  expect(1);

  var teacher = create_teacher();
  teacher.then(function() {
    console.log("DONE");
    ok(true, "done");
    start();
  },
  function() {
    console.log("FAIL");
    ok(false, "fail");
    start();
  });
});
4

1 回答 1

2

这可能与在测试模式下禁用 Ember 运行循环有关。你检查了ic-ajax吗?https://github.com/instructure/ic-ajax它以 Ember 喜欢的形式为您提供 promise 样式的 jQuery ajax 请求,即使在测试中也是如此。我引入它是为了解决我在测试中的 Ember 运行循环问题,并且到目前为止已经取得了很好的结果。

或者,您可以尝试将您teacher.then(..Ember.run.

于 2014-03-12T17:21:07.813 回答