我可以设置QUnit挂钩以在套件中的所有测试之前运行吗?

问题描述 投票:1回答:1

我正在使用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是否有它自己的方法?我在文档中看不到任何允许的设置。

javascript testing qunit ember-qunit
1个回答
0
投票

不确定是否可以满足您的需求,但是QUnit确实有一个global event/callback system for tests and modules。因此,您可以尝试通过以下方式执行此操作:

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

((还有testDone callback用于拆除该模拟游戏...)

© www.soinside.com 2019 - 2024. All rights reserved.