Gly*_*oko 5 javascript testing qunit ember-qunit
我正在使用 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);
...
}
Run Code Online (Sandbox Code Playgroud)
最近的一项功能要求在应用程序中相当高级的位置使用此服务,这意味着我现在stubMyService(hooks);几乎在每个测试中都会说。这意味着从现在开始,我必须在所有测试中都包含这个助手。有没有办法在全局范围内包含钩子?例如 RSpec 有:
config.before(:suite) do
# runs before entire suite
end
Run Code Online (Sandbox Code Playgroud)
我希望能够做类似的事情
// tests/test-helper.js
import { stubMyService } from 'helpers/mock-my-service';
QUnit.module.beforeSuite(function(hooks) {
stubMyService(hooks);
});
Run Code Online (Sandbox Code Playgroud)
有没有好的方法可以做到这一点?或者有更多的方法来解决这个问题吗?ember-qunit 有它自己的方式吗?我在文档中没有看到任何允许这样做的内容。
不确定这是否适合您的需求,但 QUnit 确实有一个用于测试和模块的全局事件/回调系统。所以你可以尝试这样做:
QUnit.testStart( ( { module, name } ) => {
mock({
type: 'GET',
url: 'https://example.com',
responseText: [
{ some: 'complex data' }
],
status: 200
});
});
Run Code Online (Sandbox Code Playgroud)
(还有一个用于拆除该模拟的testDone回调......)