[英]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. 我正在尝试使用QUnit设置ember.js来编写集成测试。
Following the directions on http://emberjs.com/guides/testing/integration/ I have: 按照http://emberjs.com/guides/testing/integration/上的说明进行操作:
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: 但是,当我运行QUnit时,出现以下错误:
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. 触发此断言错误是因为我正在应用程序初始化程序中发出一个http请求,以检查是否存在当前用户会话。 This returns a 401 error if there is no valid user.
如果没有有效的用户,这将返回401错误。 (If I force the app to always return 200 for this request, this assertion error does not occur and the tests continue as expected).
(如果我强制应用程序始终为该请求返回200,则不会发生此断言错误,并且测试将按预期进行)。
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. 我认为该应用程序和API的行为正确,是为无效用户返回了一个http错误,并且我不想更改为响应200以使我的测试正常进行。 What do I need to do to get ember and QUnit to handle a http error and assertion?
我需要做什么来使ember和QUnit处理HTTP错误和断言? From what I've read I need to wrap something with
Ember.run
, but I can't figure out what. 从我读过的
Ember.run
,我需要用Ember.run
包装一些东西,但我不知道是什么。
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 . 理想情况下,您的单元测试和集成测试应该与外部资源(例如HTTP API)隔离运行,因此模拟HTTP请求是最简单的模式 。
To get truly full-stack smoke testing, you'll want to exercise the app with browser automation tools like PhantomJS. 为了获得真正的全栈烟雾测试,您需要使用PhantomJS等浏览器自动化工具对应用程序进行练习。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.