全球之前/之后的余烬qunit测试 [英] Global beforeEach/afterEach for ember qunit tests
问题描述
我的应用程序在 localStorage
中存储有关当前会话的一些信息。因此,在所有测试文件中,每次单次测试之前或之后,我需要我的测试来清除 localStorage
。有没有办法在全球范围内定义 beforeEach
或 afterEach
回调,而不是在每个测试文件上?
我们已经将ember-qunit的模块
, moduleFor
和 moduleForComponent
几乎相同的原因。我们正在进口那些包装纸而不是ember-qunit。
另一个建议是将localStorage与服务包起来。除此服务外,不得访问localStorage。所以你可以在测试中使用它的模拟实现。
更新:
如何实现:
import {moduleFor,moduleForModel,test,only,setResolver} from'ember-qunit';
import {moduleForComponent as qunitModuleForComponent} from'ember-qunit';
函数moduleForComponent(name,description,callbacks){
//我们的实现包装qunitModuleForComponent
// eg。 init常用的服务与
}
export {
moduleFor,
moduleForComponent,
moduleForModel,
test,
only ,
setResolver
};
优点:
- 防止代码重复
- 集中单元测试管理
- 轻松为自定义需求添加新方法,例如:
moduleForValidatableComponent
,moduleForLocalStorage
缺点: p>
- ember-cli 生成正在导入的测试
ember-qunit
。开发人员必须将import语句更改为这些包装器。有时被遗忘。 (当测试失败时,开发人员记得他们需要更改导入语句。) - 对于某些测试,不需要打包。
My app stores some information about the current session in localStorage
. Therefore I need my tests to clear localStorage
before or after each single test throughout all test files. Is there a way to define a beforeEach
or afterEach
callback globally instead of on each test file?
We had wrapped ember-qunit's module
, moduleFor
and moduleForComponent
for a nearly the same reason. And we are importing those wrappers instead of ember-qunit.
Another suggestion is to wrap localStorage with a service. Never access to localStorage except this service. So you can use a mock implementation of it in tests.
Updated:
How it is realised:
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
};
Pros:
- Prevents code duplication
- Centralize unit test management
- Easy to add new methods for custom needs, such as:
moduleForValidatableComponent
,moduleForLocalStorage
Cons:
- ember-cli generates tests those are importing
ember-qunit
. Developers must change the import statements to these wrappers. It was sometimes forgotten. (When a test fails, developers remember that they need to change import statements.) - For some tests, wrapping is unnecessary.
这篇关于全球之前/之后的余烬qunit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!