Angular业力茉莉花组件测试-TestBed必须导入/声明被测组件的大子组件 [英] Angular karma jasmine component testing - TestBed have to import/declare grand children components of component under test

查看:82
本文介绍了Angular业力茉莉花组件测试-TestBed必须导入/声明被测组件的大子组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题描述: 在编写使用其他角度组件的角度组件的业力/茉莉花单元测试时,我必须在测试平台配置中以递归方式导入子组件中包含的所有组件.有没有办法我只需要导入被测组件的直接子代.

Problem Description: While writing karma/jasmine unit tests for angular component, which uses other angular components, I have to recursively import all the components included in child components, in testbed configuration. Is there a way that I only have to import direct children of the component under test.

示例代码:

父组件模板:(top-most-parent.component.html)

<h1>This is topmost parent in the current example. It has children components</h1>
<app-direct-child #scheduleEditor 
    [param1]="val1" [param2]="val2"></app-direct-child>

直接子组件模板:(app-direct-child.component.html):

<h1>This is first direct child in the current example. It itself has children 
components</h1>
<app-grand-child
    [aparam1]="ap1val" [aparam2]="ap2val"></app-grand-child>

孙子组件模板:(app-grand-child.component.html):

<h1>This is third level child in the current example</h1>
<p-radioButton name="dummyName" value="dummyVal" [label]="'testing"
                       [(ngModel)]="myModel" (onClick)="myOnClick()"
                       class="field-input"></p-radioButton>

问题: 在上面的示例代码中,在编写最上层父组件的测试用例(spec.ts)文件时,我必须导入子组件和大子组件中使用的组件.像上面的示例一样,在编写top-most-parent.component的测试用例时,我必须导入app-grand-child.component和p-radioButton,它们与被测组件没有直接关系.规格文件如下

The Question: In the above sample codes, while writing the test cases (spec.ts) file for topmost parent component, I have to import the components used in the children and grand children components. Like in the above example, while writing the test cases for top-most-parent.component, I have to import app-grand-child.component and p-radioButton, which are not directly related to the component under test. spec file is as follows

top-most-parent.component.spec.ts:请在下面的代码注释中查看我的问题

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { BrowserModule, Title } from '@angular/platform-browser';
import { Observable } from 'rxjs/Observable';

import { DirectChildComponent } from './direct-child.component';
import { GrandChildComponent } from './grand-child.component';
import { RadioButtonModule } from 'primeng/primeng';

describe('TopMostParentComponentTestSuite', () => {
  let component: TopMostParentComponent;
  let fixture: ComponentFixture<TopMostParentComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        TopMostParentComponent,
        DirectChildComponent, // I understand, this is used by the current component under test, so this should be declared
        GrandChildComponent // But why this is needed? Is there a way this can be avoided ?
        ],

      imports: [
        RadioButtonModule // Also this is being used by grand child component, why is this import needed, is there a way this can be avoided?
      ]
    }).compileComponents();

  fixture = TestBed.createComponent(TopMostParentComponent);
  component = fixture.componentInstance;
  fixture.detectChanges();
  }));

  it('should be created', () => {
    expect(component).toBeTruthy();
  });
});

推荐答案

您需要将装置和组件分配到单独的beforeEach中.像波纹管一样

You need to assign your fixture and component into a seperated beforeEach. As bellow

describe('TopMostParentComponentTestSuite', () => {
  let component: TopMostParentComponent;
  let fixture: ComponentFixture<TopMostParentComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        TopMostParentComponent,
        DirectChildComponent, // I understand, this is used by the current component under test, so this should be declared
        GrandChildComponent // But why this is needed? Is there a way this can be avoided ?
        ],

      imports: [
        RadioButtonModule // Also this is being used by grand child component, why is this import needed, is there a way this can be avoided?
      ]
    }).compileComponents();

  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(TopMostParentComponent);
     component = fixture.componentInstance;
     fixture.detectChanges();
  }));

  it('should be created', () => {
    expect(component).toBeTruthy();
  });
});

此外,如果您进行单元测试,则只需要测试组件即可.这样,您可以将其添加到TestBed.configureTestinModule

Also if you do unit testing you need only to test your component. This way you can add to your TestBed.configureTestinModule

  schemas: [ NO_ERRORS_SCHEMA ],

这篇关于Angular业力茉莉花组件测试-TestBed必须导入/声明被测组件的大子组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆