[英]Test Angular 2 Component with Constructor Parameter
Say I have an Angular 2 Component with two input parameters: 假设我有一个带有两个输入参数的Angular 2 Component:
@Component{... (omitted for clarity)}
export class SomeComponent {
@Input() a: number
@Input() b: number
}
When I want to test this component I have something like: 当我想测试这个组件时,我有类似的东西:
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
SomeComponent,
],
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SomeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
The createComponent
call does not take any parameters or allow me to call the constructor. createComponent
调用不接受任何参数或允许我调用构造函数。 How can I instantiate/test the component for various number values? 如何为各种数值实例化/测试组件?
As pointed ou by JB Nizet, when a component has @input parameters, you need to init them in the beforeEach() : ``` 正如JB Nizet指出的那样,当一个组件有@input参数时,你需要在beforeEach()中初始化它们:```
beforeEach(() => {
fixture = TestBed.createComponent(SomeComponent);
component = fixture.componentInstance;
component.a = 1;
component.b = 2;
fixture.detectChanges();
});
``` ```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.