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

时间:2017-10-22 15:16:40

标签: angular karma-runner

我的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以未定义的形式返回。

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

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您的输入标签不应该像这样格式化(没有括号和ng-model):

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

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

相关问题