Bor*_*ijk 6 testing unit-testing typescript angular
说我有一个角2组件有两个输入参数:
@Component{... (omitted for clarity)}
export class SomeComponent {
@Input() a: number
@Input() b: number
}
Run Code Online (Sandbox Code Playgroud)
当我想测试这个组件我有这样的:
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
SomeComponent,
],
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SomeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
Run Code Online (Sandbox Code Playgroud)
该createComponent调用不带任何参数或允许我调用构造函数。我怎样才能实例化/测试各种数值的组成部分?
正如 JB Nizet 所指出的,当组件具有 @input 参数时,您需要在 beforeEach() 中初始化它们:```
beforeEach(() => {
fixture = TestBed.createComponent(SomeComponent);
component = fixture.componentInstance;
component.a = 1;
component.b = 2;
fixture.detectChanges();
});
Run Code Online (Sandbox Code Playgroud)
````
| 归档时间: |
|
| 查看次数: |
6036 次 |
| 最近记录: |