簡體   English   中英

編寫QUnit集成測試時,如何處理`Ember.Application.initializer`中發出的asyc請求?

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

我正在嘗試使用QUnit設置ember.js來編寫集成測試。

按照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
  });
});

但是,當我運行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 

觸發此斷言錯誤是因為我正在應用程序初始化程序中發出一個http請求,以檢查是否存在當前用戶會話。 如果沒有有效的用戶,這將返回401錯誤。 (如果我強制應用程序始終為該請求返回200,則不會發生此斷言錯誤,並且測試將按預期進行)。

我認為該應用程序和API的行為正確,是為無效用戶返回了一個http錯誤,並且我不想更改為響應200以使我的測試正常進行。 我需要做什么來使ember和QUnit處理HTTP錯誤和斷言? 從我讀過的Ember.run ,我需要用Ember.run包裝一些東西,但我不知道是什么。

理想情況下,您的單元測試和集成測試應該與外部資源(例如HTTP API)隔離運行,因此模擬HTTP請求是最簡單的模式

為了獲得真正的全棧煙霧測試,您需要使用PhantomJS等瀏覽器自動化工具對應用程序進行練習。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM