【发布时间】:2017-11-19 00:01:38
【问题描述】:
我想测试实现 ControlValueAccessor 接口的组件,以允许在我的自定义组件中使用 [(ngModel)],但问题是通常的输入正确,但 ngModel - undefined。这是代码示例:
@Component({
template: `
<custom-component
[usualInput]="usualInput"
[(ngModel)]="modelValue"
></custom-component>`
})
class TestHostComponent {
usualInput: number = 1;
modelValue: number = 2;
}
describe('Component test', () => {
let component: TestHostComponent;
let fixture: ComponentFixture<TestHostComponent>;
let de: DebugElement;
let customComponent: DebugElement;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
CustomComponent,
],
schemas: [NO_ERRORS_SCHEMA],
}).compileComponents();
}));
});
所以,我希望我的 customComponent 中的通常输入 Input() 值等于 1(这是真的),并且 ngModel 值将等于 2,但是 ngModel = undefined 并且在调试之后我知道 ControlValueAccessor writeValue 方法不会在测试中调用环境(但它适用于浏览器)。那么我该如何解决呢?
【问题讨论】:
-
能否提供你的
custom-component的源代码?
标签: angular jasmine karma-jasmine angular2-testing