代码之家  ›  专栏  ›  技术社区  ›  Thomas Anuragh27crony

在余烬组件的生成上下文上测试属性

  •  0
  • Thomas Anuragh27crony  · 技术社区  · 7 年前

    有时,为了划分关注点,我正在开发需要生成数据传输对象(DTO)的组件。例如,如果我有某种UI组件,我想在多个上下文中重用它。我将把这个表示组件提取成非异步的东西,留下一个包装器,在需要时处理异步调用。然后,异步组件将生成调用的数据。示例用法如下:

    {{#call-ajax as |data|}}
      {{presentation-component title=data.title}}
    {{/call-ajax}}
    

    现在,为了测试,我不想使用 presentation-component 在测试中 call-ajax 组件,但我需要一些方法来断言ajax调用的结果 data .

    我最初的想法是,某种组件/助手仅可用于公开API以检查用法及其调用内容的测试。也许在测试中有这样的东西:

    this.render(hbs`
      {{#call-ajax as |data|}}
        {{test-inspector data}}
      {{/call-ajax}}
    `);
    

    虽然,我不知道如何做到这一点。

    对于如何测试 数据 对象,而不打印其中的所有基本字段并进行DOM查找?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Thomas Anuragh27crony    7 年前

    您可以通过向 this Ember中集成测试的上下文。js并将信息作为参数传递给它,作为某些机制的参数来存储这些信息以供以后检查。

    我正在使用 sinon 通过 ember-sinon 在下面的示例中,如果需要,可以采用更普通的方式进行:

    const stub = sinon.stub();
    const helper = Ember.Helper.helper(stub);
    this.register('helper:test-inspector', helper);
    
    this.render(hbs`
      {{#call-ajax as |data|}}
        {{test-inspector data}}
      {{/call-ajax}}
    `);
    
    assert.deepEqual(stub.getCall(0).args[0][0], expectedData);