【发布时间】:2018-11-22 02:33:06
【问题描述】:
我有一个这样的组件
@Component({
selector: 'app-custom-form-control',
templateUrl: '<input>',
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => SelectComponent),
multi: true
}
]
})
export class CustomFormControlComponent implements ControlValueAccessor {...}
如您所见,它是一个自定义表单控件。我在要测试的组件中使用它。
@Component({
selector: 'app-root',
templateUrl: '<div [formGroup]="form">
<app-custom-form-control formControlName="my_field"></app-custom-form-control>
</div>',
})
export class AppComponent implements OnInit, OnDestroy {...}
那么我如何模拟app-custom-form-control 进行测试?
当前的实现需要一个真正的组件...
beforeEach(async(() => {
const testRouter = new RouterStub();
const testDataService = new DataServiceStub();
TestBed.configureTestingModule({
declarations: [
AppComponent,
CustomFormControlComponent // it is a real stuff
],
imports: [
ReactiveFormsModule
],
providers: [
{ provide: Router, useValue: testRouter },
{ provide: DataService, useValue: testDataService }
],
schemas: [ NO_ERRORS_SCHEMA ]
}).compileComponents();
}));
否则(不声明组件)我收到错误Failed: No value accessor for form control with name: app-custom-form-control
【问题讨论】:
标签: angular unit-testing testing custom-controls angular-reactive-forms