全局beforeEach / afterEach for ember qunit tests

时间:2016-10-18 14:45:26

标签: ember.js ember-cli qunit ember-qunit ember-testing

我的应用会在hour, count中存储有关当前会话的一些信息。因此,我需要在所有测试文件中的每次单个测试之前或之后清除first_character, second_character我的测试。有没有办法全局定义localStoragelocalStorage回调而不是每个测试文件?

1 个答案:

答案 0 :(得分:2)

我们已经包装了ember-qunit的modulemoduleFormoduleForComponent,原因几乎相同。我们正在导入那些包装器而不是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
};

优点:

  • 防止代码重复
  • 集中单元测试管理
  • 轻松添加自定义需求的新方法,例如:moduleForValidatableComponentmoduleForLocalStorage

缺点:

  • ember-cli 生成导入ember-qunit的测试。开发人员必须将import语句更改为这些包装器。它有时被遗忘了。 (当测试失败时,开发人员会记住他们需要更改import语句。)
  • 对于某些测试,不需要包装。
相关问题