我有一个服务中的私有函数,看起来像这样。如何编写该按钮的单元测试click事件?该按钮位于对话框内。
1)那能进行单元测试吗? 2)由于我提到了这是在一个服务中,点击事件也涉及其中,所以应该像组件一样进行测试吗?这意味着我必须设置TestBed,以便实际点击该按钮,如果是,如何操作?或者是否有更正确的方法来编写它的单元测试,如何操作?
谢谢!
public callOpenDialog(){
openDialog(formA, dataView, {}, true);
}
private openDialog(formA: FormA, dataView: DataView, placeholder: ViewContainerRef, okToAll: boolean): Observable<ResponseRequiredResult> {
return new Observable<ResponseRequiredResult>(observer => {
let dlgComponent: ResponseRequiredModalFormComponent;
let dialog: DialogRef<ResponseRequiredModalFormComponent>;
const dialogId = 'ResponseRequired';
let buttons = [
{
id: this.lmUniqueIdService.uniqueId([formA.busFormA.name, dialogId, DialogButtonKey.OK]),
text: Locale.translate(DialogButtonKey.OK),
validate : false,
isDefault: true,
click: (e, modal) => {
if (this.validateResponseRequiredForm(formA.busFormA, dlgComponent.formComponent.formModel)) {
dialog.close(<ResponseRequiredResult> {
result: ResponseRequiredDialogResult.RESPONSE_OK,
specFields: formA.busFormA.fields, model: dlgComponent.formComponent.formModel
});
}
}
}
];
dialog = modalDialogService
.modal(ResponseRequiredModalFormComponent, placeholder)
.buttons(buttons)
.title(Locale.translate('ResponseRequired'))
.isAlert(true)
.open();
}
click
回调函数提取到自己的函数中并测试该函数。你不需要测试点击的触发,这应该在ModalDialogService
或ResponseRequiredModalFormComponent
中进行测试。 - AntoineB