angular4 unit test使用select change事件更新ngModel

时间:2017-08-23 17:24:58

标签: angular unit-testing

似乎无法使用ngModel控件change事件测试select绑定:

成分:

@Component({
    selector: 'my-area',
    templateUrl: 'app/test.component.html'
})
export class TestComponent {
  selectedFred;
  freds = [{name:"kruger"},{name:"Durst"}];
}

模板:

<select [(ngModel)]="selectedFred">
    <option *ngFor="let f of freds" [ngValue]="f">{{f.name}}</option>
</select>

试验:

   it('should allow us to set a bound select field', fakeAsync(() => {
     let input = fixture.debugElement.query(By.css('select')).nativeElement;
      console.error(input);
        input.value = component.freds[1];
        input.selectedValue = component.freds[1];
        input.dispatchEvent(new Event('change'));
        tick();

        expect(component.selectedFred.name).toEqual('Durst');
    }));

plunkr:https://plnkr.co/edit/ZXJ6bsjfu78d4m928U8S?p=preview 我究竟做错了什么?

使用input控件的类似单元测试就像魅力一样,请参阅:https://plnkr.co/edit/0ELwf5W26XoBlKa5Y5xA?p=preview

0 个答案:

没有答案