angular - 在 Karma 上测试异步用户界面操作
问题描述
我在下面进行了以下测试,其中我更新了一个文本字段,之后进行了 API 调用,然后,我希望另一个文本字段根据 API 响应具有相同的值。但是,它一直失败,错误Expected '0' to equal '2'.
是我期望根据 API 的响应得到 2 = 2 的结果。知道如何解决这个问题吗?
Stackblitz 在这里:https ://stackblitz.com/edit/http-get-post-dgcuur
测试
it('should successfuly convert A to A', fakeAsync(() => {
fixture.detectChanges();
fixture.whenStable().then(() => {
let fromAmountValueEl = fixture.debugElement.query(By.css('#fromAmountValue')).nativeElement;
expect(fromAmountValueEl.value).toEqual('0');
let toAmountValueEl = fixture.debugElement.query(By.css('#toAmountValue')).nativeElement;
expect(toAmountValueEl.value).toEqual('0');
fromAmountValueEl.value = '2'
fromAmountValueEl.dispatchEvent(new Event('input'));
fromAmountValueEl.dispatchEvent(new KeyboardEvent('keyup', {
'key': 'Enter'
}));
tick();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(fromAmountValueEl.value).toEqual('2');
expect(toAmountValueEl.value).toEqual('2');
});
});
}));
看法
<input class="form-control form-control-lg" type="number" placeholder="0" [(ngModel)]="fromAmount"
(keyup)="updateRates()" id="fromAmountValue">
<select class="form-control form-control-lg" id="fromCurrency">
<option value="GBP">GBP</option>
</select>
<input class="form-control form-control-lg" type="number" placeholder="0" [(ngModel)]="toAmount"
(keyup)="updateRates()" id="toAmountValue">
<select class="form-control form-control-lg" [(ngModel)]="toCurrency" id="toCurrency">
<option value="GBP">GBP</option>
</select>
控制器
updateRates() {
this.postSubscription = this.post().subscribe((data) => {
if(data) {
this.result = data;
if (this.toCurrency == "GBP") {
this.toAmount = this.fromAmount;
}
}
});
}
解决方案
推荐阅读
- typescript - 类型'typeof“..node_modules/@types/three/index”'上不存在属性'DecalGeometry'
- java - JavaFx ListView 不显示文本,但在 cellFactory 之后工作
- python - eval(dir()[0]) 在 python 中做了什么
- python - json.tool shell 返回 1 我丢失了我的 vim 缓冲区
- python-3.x - 从 csv 文件中绘制 3D 点,Python?
- django - django 基于类的视图、CreateView 和 UpdateView 是否在后台清理数据?
- php - PDO 将多个插入语句绑定到映射的数组值
- scala - 有没有像 Collectable typeclass 这样的东西?
- vb.net - SSRS - 使用带有“If”语句和多个参数值的表达式
- php - CORS Preflight 403,但我有正确的标题