Angular 2单元测试组件,模拟ContentChildren

凯捷·沃特纳尔

我正在Angular 2 RC4中实现一个向导组件,现在我正在尝试编写som单元测试。Angular 2中的单元测试开始有据可查,但是我根本无法找出如何在组件中模拟内容查询的结果

该应用程序具有2个组件(除了应用程序组件之外),即WizardComponent和WizardStepComponent。应用程序组件(app.ts)定义了向导及其模板中的步骤:

 <div>
  <fa-wizard>
    <fa-wizard-step stepTitle="First step">step 1 content</fa-wizard-step>
    <fa-wizard-step stepTitle="Second step">step 2 content</fa-wizard-step>
    <fa-wizard-step stepTitle="Third step">step 3 content</fa-wizard-step>
  </fa-wizard>
</div>

WizardComponent(wizard-component.ts)通过使用ContentChildren查询获得对步骤的引用。

@Component({
selector: 'fa-wizard',
template: `<div *ngFor="let step of steps">
            <ng-content></ng-content>
          </div>
          <div><button (click)="cycleSteps()">Cycle steps</button></div>`

})
export class WizardComponent implements AfterContentInit {
    @ContentChildren(WizardStepComponent) steps: QueryList<WizardStepComponent>;
....
}

问题是如何在单元测试中模拟步骤变量:

describe('Wizard component', () => {
  it('should set first step active on init', async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
    return tcb
    .createAsync(WizardComponent)
    .then( (fixture) =>{
        let nativeElement = fixture.nativeElement;
        let testComponent: WizardComponent = fixture.componentInstance;

        //how to initialize testComponent.steps with mock data?

        fixture.detectChanges();

        expect(fixture.componentInstance.steps[0].active).toBe(true);
    });
  })));
});

我创建了一个插件,实现了一个非常简单的向导来演示该问题。Wizard-component.spec.ts文件包含单元测试。

如果有人能指出正确的方向,我将不胜感激。

凯捷·沃特纳尔

多亏了drewmoore这个问题上的回答,我才能够开始工作。

关键是创建一个用于测试的包装器组件,该组件在其模板中指定向导和向导步骤。然后,Angular将为您执行内容查询并填充变量。

编辑:实现是针对Angular 6.0.0-beta.3

我的完整测试实现如下所示:

  //We need to wrap the WizardComponent in this component when testing, to have the wizard steps initialized
  @Component({
    selector: 'test-cmp',
    template: `<fa-wizard>
        <fa-wizard-step stepTitle="step1"></fa-wizard-step>
        <fa-wizard-step stepTitle="step2"></fa-wizard-step>
    </fa-wizard>`,
  })
  class TestWrapperComponent { }

  describe('Wizard component', () => {
    let component: WizardComponent;
    let fixture: ComponentFixture<TestWrapperComponent>;

    beforeEach(async(() => {
      TestBed.configureTestingModule({
        schemas: [ NO_ERRORS_SCHEMA ],
        declarations: [
          TestWrapperComponent,
          WizardComponent,
          WizardStepComponent
        ],
      }).compileComponents();
    }));

    beforeEach(() => {
      fixture = TestBed.createComponent(TestWrapperComponent);
      component = fixture.debugElement.children[0].componentInstance;
    });

    it('should set first step active on init', () => {
      expect(component.steps[0].active).toBe(true);
      expect(component.steps.length).toBe(3);
    });
  });

如果您有更好的/其他解决方案,也非常欢迎您添加答案。我将这个问题待一段时间。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Angular 9单元测试:如何模拟测试组件的导入?

来自分类Dev

组件的 Angular 2 单元测试

来自分类Dev

在单元测试中模拟Angular 1.5组件

来自分类Dev

在Angular单元测试中,如何模拟整个组件而不是仅模拟类?

来自分类Dev

Angular:在单元测试中模拟HttpClient

来自分类Dev

Angular 2单元测试

来自分类Dev

如何在 Angular 2 单元测试中测试共享组件的方法?

来自分类Dev

angular2-如何在http.post单元测试上模拟错误

来自分类Dev

Angular JS单元测试模拟文档

来自分类Dev

如何在Angular单元测试中模拟/触发$ routeChangeSuccess?

来自分类Dev

在Angular单元测试中模拟异步Web服务

来自分类Dev

如何在Angular单元测试中模拟/触发$ routeChangeSuccess?

来自分类Dev

在 Angular 4 单元测试中模拟空闲

来自分类Dev

Angular 6 单元测试静态模拟响应

来自分类Dev

具有第三方组件的Angular 2单元测试

来自分类Dev

具有第三方组件的Angular 2单元测试

来自分类Dev

单元测试Angular 2错误:在注入Router之前引导至少一个组件?

来自分类Dev

包含异步的拆分 Angular 组件单元测试

来自分类Dev

由于构造函数中的组件,Angular 单元测试失败。

来自分类Dev

来自组件的 Angular 单元测试 spyOn 服务

来自分类Dev

模拟要在Angular单元测试中订阅的模拟服务的Subject属性

来自分类Dev

Angular 单元测试服务

来自分类Dev

单元测试 Angular 服务

来自分类Dev

如何使用路由器对Angular 2 RC4组件进行单元测试

来自分类Dev

使用angular 2添加Firebase的单元测试

来自分类Dev

Angular 2和带有服务的单元测试

来自分类常见问题

使用@Input()进行Angular2单元测试

来自分类Dev

Angular2单元测试+创建接口对象

来自分类Dev

Angular2在单元测试中注入ElementRef

Related 相关文章

  1. 1

    Angular 9单元测试:如何模拟测试组件的导入?

  2. 2

    组件的 Angular 2 单元测试

  3. 3

    在单元测试中模拟Angular 1.5组件

  4. 4

    在Angular单元测试中,如何模拟整个组件而不是仅模拟类?

  5. 5

    Angular:在单元测试中模拟HttpClient

  6. 6

    Angular 2单元测试

  7. 7

    如何在 Angular 2 单元测试中测试共享组件的方法?

  8. 8

    angular2-如何在http.post单元测试上模拟错误

  9. 9

    Angular JS单元测试模拟文档

  10. 10

    如何在Angular单元测试中模拟/触发$ routeChangeSuccess?

  11. 11

    在Angular单元测试中模拟异步Web服务

  12. 12

    如何在Angular单元测试中模拟/触发$ routeChangeSuccess?

  13. 13

    在 Angular 4 单元测试中模拟空闲

  14. 14

    Angular 6 单元测试静态模拟响应

  15. 15

    具有第三方组件的Angular 2单元测试

  16. 16

    具有第三方组件的Angular 2单元测试

  17. 17

    单元测试Angular 2错误:在注入Router之前引导至少一个组件?

  18. 18

    包含异步的拆分 Angular 组件单元测试

  19. 19

    由于构造函数中的组件,Angular 单元测试失败。

  20. 20

    来自组件的 Angular 单元测试 spyOn 服务

  21. 21

    模拟要在Angular单元测试中订阅的模拟服务的Subject属性

  22. 22

    Angular 单元测试服务

  23. 23

    单元测试 Angular 服务

  24. 24

    如何使用路由器对Angular 2 RC4组件进行单元测试

  25. 25

    使用angular 2添加Firebase的单元测试

  26. 26

    Angular 2和带有服务的单元测试

  27. 27

    使用@Input()进行Angular2单元测试

  28. 28

    Angular2单元测试+创建接口对象

  29. 29

    Angular2在单元测试中注入ElementRef

热门标签

归档