2017-04-10 87 views
1

我想在角2 我的HTML代碼來測試與噶茉莉花的HTML單選按鈕就是這樣Angular2-單選按鈕測試

<label class="color-dark display-block" *ngFor="let userGroup of userGroups"> 
    <input type="radio" id="stateRadio" name="statusRadio(click)="selectedGroup(seGrp = statusLabel.value)" [checked]="statusLabel.value === selectionStatus">{{statusLabel.value}} 
</label> 

規格

it('should have defined number of userGroup radio buttons',() => { 
    el = fixture.debugElement.query(By.css('#stateRadio')).nativeElement 
    expect(el.click()).toBeTruthy(); 
}); 

我(error) - 類型錯誤:null不是一個對象(評估'fixture.debugElement.query(platform_browser_1.By.css('#stateRadio'))nativeElement')

在規範中,類型是HTMLElement和I' m id到By。 css()作爲css class-color-dark顯示塊對其他輸入類型也是通用的。我該如何解決這個問題?

+1

HTML元素ID應該是唯一的,FYI。 –

+0

@AluanHaddad是的。但我的方法不起作用 – Protagonist

+0

我建議'id =「stateRadio」它不是一個可靠的選擇器,因爲它是在'* ngFor'中創建的。 –

回答

0

沒有看到組件的邏輯,我不得不猜測,羣組數組是空的或不確定的,所以什麼都沒有呈現,即:

fixture.debugElement.query(By.css('#stateRadio')) 

返回null。參考文獻: