相关疑难解决方法(0)

角度单位测试输入值

我一直在阅读用于单元测试的官方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"

unit-testing angular2-testing angular

42
推荐指数
3
解决办法
5万
查看次数

在angular2测试中,fakeAsync和async有什么区别

我知道tick()函数使用fakeAsync.而且我也可以使用fixture.whenStable().then()和async以及fakeAsync.我想知道两者的确切用例.任何人都可以用例子解释这一点

注意:我想在两种方案中使用虚假服务或存根.

javascript jasmine karma-jasmine angular2-testing angular

20
推荐指数
1
解决办法
7995
查看次数

角4单元测试:de.query(By.css(...))与de.nativeElement.querySelector(...)的本机Web API

使用el = de.query(By.css('h2')).nativeElement;本机元素API 是否有任何优势el = de.nativeElement.querySelector('h2');?它们提供相同的结果。

刚从Angular 4单元测试开始,想知道是否存在任何性能差异,或者由于它们完成相同的工作而需要在其他方面使用的理由。我不确定我了解使用By.css(...)的便利性,或者您可能会在哪种情况/原因下使用另一种方法。

unit-testing angular

3
推荐指数
1
解决办法
470
查看次数