【问题标题】:Global beforeEach/afterEach for ember qunit tests用于 ember qunit 测试的全局 beforeEach/afterEach
【发布时间】:2017-02-27 21:51:28
【问题描述】:

我的应用程序将有关当前会话的一些信息存储在localStorage 中。因此,我需要我的测试在所有测试文件中的每个单个测试之前或之后清除localStorage。有没有办法在全局范围内而不是在每个测试文件上定义 beforeEachafterEach 回调?

【问题讨论】:

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


    【解决方案1】:

    出于几乎相同的原因,我们包装了 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 的测试。开发人员必须将导入语句更改为这些包装器。有时会被遗忘。 (当测试失败时,开发人员会记住他们需要更改导入语句。)
    • 对于某些测试,不需要包装。

    【讨论】:

    • 感谢有关使用服务包装 localStorage 的提示。但我仍然更喜欢你提到的关于包装这些 ember-qunit 方法的内容,因为这个功能可能会变得很方便,超出了我目前对它的具体需求。你能详细说明一下这种包装是如何工作的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-03
    • 1970-01-01
    相关资源
    最近更新 更多