我一直在阅读用于单元测试的官方Angular2文档(https://angular.io/docs/ts/latest/guide/testing.html),但我正在努力设置组件的输入字段值,以便它反映在组件属性中(通过ngModel绑定).屏幕在浏览器中工作正常,但在单元测试中我似乎无法设置字段值.
我正在使用下面的代码."夹具"已正确初始化,因为其他测试工作正常."comp"是我的组件的实例,输入字段通过ngModel绑定到"user.username".
it('should update model...', async(() => {
let field: HTMLInputElement = fixture.debugElement.query(By.css('#user')).nativeElement;
field.value = 'someValue'
field.dispatchEvent(new Event('input'));
fixture.detectChanges();
expect(field.textContent).toBe('someValue');
expect(comp.user.username).toBe('someValue');
}));
Run Code Online (Sandbox Code Playgroud)
我的Angular2版本: "@angular/core": "2.0.0"
我知道tick()函数使用fakeAsync.而且我也可以使用fixture.whenStable().then()和async以及fakeAsync.我想知道两者的确切用例.任何人都可以用例子解释这一点
注意:我想在两种方案中使用虚假服务或存根.
使用el = de.query(By.css('h2')).nativeElement;本机元素API 是否有任何优势el = de.nativeElement.querySelector('h2');?它们提供相同的结果。
刚从Angular 4单元测试开始,想知道是否存在任何性能差异,或者由于它们完成相同的工作而需要在其他方面使用的理由。我不确定我了解使用By.css(...)的便利性,或者您可能会在哪种情况/原因下使用另一种方法。