问题描述: 在为使用其他角度组件的角度组件编写karma / jasmine单元测试时,我必须以testbed配置递归导入子组件中包含的所有组件。有没有办法我只需要导入被测组件的直接子节点。
示例代码:
父组件模板:( 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>
Grand Child Component Template:(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,它们与被测试的组件没有直接关系。 spec文件如下
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();
});
});
答案 0 :(得分:2)
您需要将夹具和组件分配到单独的beforeEach中。如吼叫
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
schemas: [ NO_ERRORS_SCHEMA ],