【发布时间】:2017-02-27 21:51:28
【问题描述】:
我的应用程序将有关当前会话的一些信息存储在localStorage 中。因此,我需要我的测试在所有测试文件中的每个单个测试之前或之后清除localStorage。有没有办法在全局范围内而不是在每个测试文件上定义 beforeEach 或 afterEach 回调?
【问题讨论】:
标签: ember.js ember-cli qunit ember-qunit ember-testing
我的应用程序将有关当前会话的一些信息存储在localStorage 中。因此,我需要我的测试在所有测试文件中的每个单个测试之前或之后清除localStorage。有没有办法在全局范围内而不是在每个测试文件上定义 beforeEach 或 afterEach 回调?
【问题讨论】:
标签: ember.js ember-cli qunit ember-qunit ember-testing
出于几乎相同的原因,我们包装了 ember-qunit 的 module、moduleFor 和 moduleForComponent。我们正在导入这些包装器而不是 ember-qunit。
另一个建议是使用服务包装 localStorage。除此服务外,永远不要访问 localStorage。所以你可以在测试中使用它的模拟实现。
更新:
它是如何实现的:
import { moduleFor, moduleForModel, test, only, setResolver } from 'ember-qunit';
import { moduleForComponent as qunitModuleForComponent } from 'ember-qunit';
function moduleForComponent(name, description, callbacks) {
//our implementation that wraps "qunitModuleForComponent"
//eg. init commonly used services vs.
}
export {
moduleFor,
moduleForComponent,
moduleForModel,
test,
only,
setResolver
};
优点:
moduleForValidatableComponent、moduleForLocalStorage 缺点:
ember-qunit 的测试。开发人员必须将导入语句更改为这些包装器。有时会被遗忘。 (当测试失败时,开发人员会记住他们需要更改导入语句。)【讨论】: