Angular2 NgModel not getting value in Jasmine test

后端 未结 2 1426
天命终不由人
天命终不由人 2021-02-05 17:54

I am using template-driven forms in Angular 2, and I\'m trying to develop them test-first. I\'ve scoured this site and the rest of the internet and I\'ve tried basically everyth

2条回答
  •  悲&欢浪女
    2021-02-05 18:40

    I think whenStable should do the trick in your case:

    it('submits the value',() => {
        fixture.whenStable().then(() => {
            // ngModel should be available here 
        })
    });
    

    More information here: https://angular.io/docs/ts/latest/guide/testing.html#!#when-stable

    EDIT: The ValueAccessor seems not to notice the change if you directly manipulate the value of the DOM-element. But it should notice, if you write the value directly with the ValueAccessor:

    const skillCount= fixture.debugElement.query(By.directive(NgModel));
    const ngModel= skillCount.injector.get(NgModel);
    
    ngModel.valueAccessor.writeValue('10')
    

提交回复
热议问题