input属性的工作方式与任何变量一样。在beforeach中,可以将其设置为值
beforeEach(() => {
fixture = TestBed.createComponent(ExplorerChartViewComponent);
component = fixture.componentInstance;
component.currentChart = someChart; // set input before first detectChanges
fixture.detectChanges();
});
你可以阅读更多关于这个
here
. 我更喜欢
this approach
.
@Component({
selector: 'app-testhost-chart',
template: `<app-chart [currentChart]=chart></app-chart>`, // or whatever your Chart Component Selector is
})
export class TestHostComponent {
chart = new Chart();
}
declarations: [ChartComponent, TestHostComponent ],
...
beforeEach(() => {
fixture = TestBed.createComponent(TestHostComponent );
component = fixture.debugElement.children[0].componentInstance;
fixture.detectChanges();
});
不过,我想我看到了另外两个问题。尤其是在分配图形时
-
你把
declarations: [ChartComponent],
但是创造
fixture = TestBed.createComponent(ExplorerChartViewComponent);
我想应该是的
TestBed.createComponent(ChartComponent)
-
你的html有
<plot [data]="graph.data" [layout]="graph.layout"></plot>
表示未声明的打印组件。您需要为plot声明一个组件。我建议您做一些与TestHostComponent非常相似的事情,但是它与真正的PlotComponent具有所有相同的公共属性,这样您就不会将PlotComponent的真正功能和依赖性引入到ChartComponent的单元测试中。