簡體   English   中英

我可以設置一個 QUnit 鈎子在套件中的所有測試之前運行嗎?

[英]Can I setup a QUnit hook to run before all tests in a suite?

我正在使用 ember-qunit 並在我的應用程序中有一項服務,可以進行一些重要的 api 調用。 為了解決這個問題,我使用了一個測試助手:

// tests/helpers/mock-my-service.js
import { mock } from 'ember-data-factory-guy';

export function stubMyService(hooks) {
  hooks.beforeEach(function() {
    mock({
      type: 'GET',
      url: 'https://example.com',
      responseText: [
        { some: 'complex data' }
      ],
      status: 200
    });
  });
}

// tests/some-test.js
import { stubMyService } from 'helpers/mock-my-service';

module('Integration | Component | Whatever', function(hooks) {
  stubMyService(hooks);
  ...
}

最近的一項功能要求在應用程序中相當高級的位置使用此服務,這意味着我現在說的是stubMyService(hooks); 在幾乎所有的測試中。 這意味着從現在開始,我必須在所有測試中都包含這個助手。 有沒有辦法在全局范圍內包含鈎子? 例如 RSpec 有:

config.before(:suite) do
  # runs before entire suite
end

我希望能夠做類似的事情

// tests/test-helper.js
import { stubMyService } from 'helpers/mock-my-service';

QUnit.module.beforeSuite(function(hooks) {
  stubMyService(hooks);
});

有沒有好的方法可以做到這一點? 或者有更多的方法來解決這個問題嗎? ember-qunit 有它自己的方式嗎? 我在文檔中沒有看到任何允許這樣做的內容。

不確定這是否適合您的需求,但 QUnit 確實有一個用於測試和模塊全局事件/回調系統 所以你可以嘗試這樣做:

QUnit.testStart( ( { module, name } ) => {
  mock({
    type: 'GET',
    url: 'https://example.com',
    responseText: [
      { some: 'complex data' }
    ],
    status: 200
  });
});

(還有一個testDone回調用於拆除那個模擬......)

暫無
暫無

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

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