简体   繁体   English

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

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

I'm using ember-qunit and have a service in my app that makes some nontrivial api calls.我正在使用 ember-qunit 并在我的应用程序中有一项服务,可以进行一些重要的 api 调用。 To handle this, I'm using a test helper:为了解决这个问题,我使用了一个测试助手:

// 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);
  ...
}

Recently a feature required this service to be used in a fairly high level location in the app, meaning that I'm now saying stubMyService(hooks);最近的一项功能要求在应用程序中相当高级的位置使用此服务,这意味着我现在说的是stubMyService(hooks); in almost every test.在几乎所有的测试中。 This means that I'd have to include this helper for all tests from now on.这意味着从现在开始,我必须在所有测试中都包含这个助手。 Is there a way to include hooks globally?有没有办法在全局范围内包含钩子? For example RSpec has:例如 RSpec 有:

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

I'd love to be able to do something like我希望能够做类似的事情

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

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

Is there a good way to do this?有没有好的方法可以做到这一点? Or is there a more qunity way of approacing this?或者有更多的方法来解决这个问题吗? Does ember-qunit have it's own way of doing this? ember-qunit 有它自己的方式吗? I don't see anything in the documentation allowing for this.我在文档中没有看到任何允许这样做的内容。

Not sure if this works for your needs, but QUnit does have a global event/callback system for tests and modules .不确定这是否适合您的需求,但 QUnit 确实有一个用于测试和模块全局事件/回调系统 So you could try to do this in that:所以你可以尝试这样做:

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

(There's also a testDone callback for tearing down that mock...) (还有一个testDone回调用于拆除那个模拟......)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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