单元测试 – 检查Angular单元测试中的单选按钮
发布时间:2020-12-17 10:24:38 所属栏目:安全 来源:网络整理
导读:我在Angular中有一个使用此 HTML的组件: div class="row" label for="tipo" class="column"Tipo: /label div *ngFor="let tipo of tipos" class="column" input type="radio" name="tipo" [value]="tipo.id" [(ngModel)]="movimiento.tipo" (ngModelChange)
我在Angular中有一个使用此
HTML的组件:
<div class="row"> <label for="tipo" class="column">Tipo: </label> <div *ngFor="let tipo of tipos" class="column"> <input type="radio" name="tipo" [value]="tipo.id" [(ngModel)]="movimiento.tipo" (ngModelChange)="alCambiarTipo()"> <span class="{{tipo.texto | lowercase}}">{{ tipo.texto }}</span> </div> </div> 它有两个单选按钮,在更改时它会在我的组件中触发一个功能.在我的测试中,我想检查第二个单选按钮以测试我的函数被调用.我已经尝试过这段代码,但它不起作用: it('should call alCambiarTipo on radio button change',() => { spyOn(component,'alCambiarTipo').and.callThrough(); let options: DebugElement[] = fixture.debugElement.queryAll(By.css('input[type="radio"]')); let secondOption: HTMLInputElement = options[1].nativeElement; secondOption.checked = true; expect(component.alCambiarTipo).toHaveBeenCalled(); }); 我也尝试在输入上使用.click(),它也无法正常工作.我该怎么做才能触发我的功能?谢谢. PS:我还尝试更改模型添加component.movi??miento.tipo = 1;并调用fixture.detectChanges(),它也无法正常工作.
因为你使用了错误的事件.应该是改变事件.
options[1].triggerEventHandler('change',{ target: options[1].nativeElement }); 你甚至不需要设置支票 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |