我的应用程序将有关当前会话的一些信息存储在localStorage
. 因此,我需要localStorage
在所有测试文件的每个测试之前或之后清除我的测试。有没有办法在全局而不是在每个测试文件上定义一个beforeEach
或afterEach
回调?
1 回答
2
我们包装了 ember-qunit 的,module
原因几乎相同。我们正在导入这些包装器而不是 ember-qunit。moduleFor
moduleForComponent
另一个建议是使用服务包装 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-cli生成正在导入的测试
ember-qunit
。开发人员必须将导入语句更改为这些包装器。有时会被遗忘。(当测试失败时,开发人员会记住他们需要更改导入语句。) - 对于某些测试,包装是不必要的。
于 2016-10-19T06:14:10.427 回答