简体   繁体   中英

How do you handle a asyc request made in `Ember.Application.initializer` when writing QUnit integration test?

I am trying to setup ember.js with QUnit to write integration tests.

Following the directions on http://emberjs.com/guides/testing/integration/ I have:

document.write('<div id="ember-testing-container"><div id="ember-testing"></div></div>');

App.rootElement = '#ember-testing';
App.setupForTesting();
App.injectTestHelpers();

module("Integration Tests", {
  setup: function() {
    App.reset();
  }
});

test("root lists first page of posts", function(){
  visit("/").then(function() {
    equal(find(".post").length, 5, "The first page should have 5 posts");
    // Assuming we know that 5 posts display per page and that there are more than 5 posts
  });
});

However when I run QUnit I get the following error:

Assertion failed: You have turned on testing mode, which disabled the 
run-loop's autorun. You will need to wrap any code with asynchronous 
side-effects in an Ember.run 

This assertion error is triggered because I am making a http request in the app initializer to check if there is a current user session. This returns a 401 error if there is no valid user. (If I force the app to always return 200 for this request, this assertion error does not occur and the tests continue as expected).

I believe the app and API is behaving correctly by returning a http error for an invalid user, and I don't want to change to response to a 200 just to get my tests working. What do I need to do to get ember and QUnit to handle a http error and assertion? From what I've read I need to wrap something with Ember.run , but I can't figure out what.

Your unit and integration tests should ideally run in isolation from external resources (like an HTTP API), so mocking HTTP requests is the easiest pattern .

To get truly full-stack smoke testing, you'll want to exercise the app with browser automation tools like PhantomJS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM