Angular (角度单元测试)如何在Jasmin中模拟输入属性?
我目前正在尝试模拟角度单元测试的输入属性。很遗憾,我无法获得进一步的信息,并多次收到以下错误消息: TypeError:无法读取未定义的属性“data” 我的HTML模板如下所示Angular (角度单元测试)如何在Jasmin中模拟输入属性?,angular,typescript,unit-testing,karma-jasmine,angular-unit-test,Angular,Typescript,Unit Testing,Karma Jasmine,Angular Unit Test,我目前正在尝试模拟角度单元测试的输入属性。很遗憾,我无法获得进一步的信息,并多次收到以下错误消息: TypeError:无法读取未定义的属性“data” 我的HTML模板如下所示 <div class="container-fluid"> <div class="row"> <div class="col-12"> <plot [data]="graph.data" [layout]="graph.layout"></
<div class="container-fluid">
<div class="row">
<div class="col-12">
<plot [data]="graph.data" [layout]="graph.layout"></plot>
</div>
</div>
</div>
我的单元测试目前看起来非常基本,但仍然抛出了提到的错误:
describe('ChartComponent', () => {
let component: ChartComponent;
let fixture: ComponentFixture<ChartComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ChartComponent],
imports: [
// My imports
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ChartComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
description('ChartComponent',()=>{
let组件:chart组件;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[图表组件],
进口:[
//我的进口
]
})
.compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(ChartComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
});
它('应该创建',()=>{
expect(component.toBeTruthy();
});
});
我尝试了不同的方法来模拟数据属性和currentChart
@Input
实现这一点并修复单元测试的正确方法是什么?输入属性的工作方式与任何变量一样。在beforeach中,可以将其设置为值
beforeEach(() => {
fixture = TestBed.createComponent(ExplorerChartViewComponent);
component = fixture.componentInstance;
component.currentChart = someChart; // set input before first detectChanges
fixture.detectChanges();
});
你可以阅读更多关于这方面的内容。我更喜欢
使用我的首选方法,您将拥有一个类似于
@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();
}
然而,我想我看到了你们可能遇到的另外两个问题。尤其是在分配图形时
声明:[ChartComponent],
但创建fixture=TestBed.createComponent(ExplorerChartViewComponent)
我认为它应该TestBed.createComponent(ChartComponent)
,除非这是一个复制/粘贴问题
,表示未声明的绘图组件。您需要为绘图声明一个组件。我建议您做一些与TestHostComponent非常相似的事情,但它具有与真正的PlotComponent相同的公共属性,这样您就不会将PlotComponent的真正功能和依赖项引入ChartComponent的单元测试中输入属性的工作方式与任何变量一样。在beforeach中,可以将其设置为值
beforeEach(() => {
fixture = TestBed.createComponent(ExplorerChartViewComponent);
component = fixture.componentInstance;
component.currentChart = someChart; // set input before first detectChanges
fixture.detectChanges();
});
你可以阅读更多关于这方面的内容。我更喜欢
使用我的首选方法,您将拥有一个类似于
@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();
}
然而,我想我看到了你们可能遇到的另外两个问题。尤其是在分配图形时
声明:[ChartComponent],
但创建fixture=TestBed.createComponent(ExplorerChartViewComponent)
我认为它应该TestBed.createComponent(ChartComponent)
,除非这是一个复制/粘贴问题
,表示未声明的绘图组件。您需要为绘图声明一个组件。我建议您做一些与TestHostComponent非常相似的事情,但它具有与真正的PlotComponent相同的公共属性,这样您就不会将PlotComponent的真正功能和依赖项引入ChartComponent的单元测试中在
expect
block之前的spec文件中执行console.log(component.graph)
时会得到什么?在expect
block之前的spec文件中执行console.log(component.graph)
时会得到什么?感谢您的详细解释。现在测试运行良好,就像你说的:错误的组件声明是一个输入错误。非常感谢!!关于角度输入测试,我找到了最好的解释!谢谢你的详细解释。现在测试运行良好,就像你说的:错误的组件声明是一个输入错误。非常感谢!!关于角度输入测试,我找到了最好的解释!