繁体   English   中英

NgModel在角度测试中未更新类型无线电的输入

[英]NgModel not Updating for Input of Type Radio in Angular Testing

我在Angular 2应用程序中input以下内容:

<input 
    id="radio-{{geo.id}}"
    name="radio-geos"
    type="radio" 
    [value]="geo.value"
    [(ngModel)]="selectedGeo">

selectedGeo绑定到public selectedGeo: string;

我为输入元素编写了以下内容:

it('should select the US Home Region', (done) => {
        // Arrange
        const component = TestBed.createComponent(regionComponent);

        // Act
        component.detectChanges();
        const usGeoRadioButton = component.debugElement.query(By.css('#radio-0'));                

        usGeoRadioButton.triggerEventHandler('click', null);
        component.detectChanges();
        const selectedGeo = component.componentInstance.selectedGeo;

        // Assert
        component.whenStable().then(() => {
            expect(selectedGeo).toBe('US');
            done();
        });
    });

在Karma中运行测试后,我注意到未单击单选按钮,并且未更新selectedGeo 我的测试失败,因为selectedGeo返回为undefined。

我不确定为什么会这样,并且我已经对dispatchEvent以及[(ngModel)]的更新方式进行了一些研究,但我无法弄清楚。

有什么建议么?

您的输入标签不应采用这种格式(在value和ng-model周围没有方括号):

<input type="radio" ng-model="string" value="string" [name="string"]...>

Doc链接: https : //docs.angularjs.org/api/ng/input/input%5Bradio%5D

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM