angular - 错误:预期的间谍 updateRates 已被调用
问题描述
Expected spy updateRates to have been called
每当我运行以下测试时,我都会收到以下错误:可能是什么原因造成的?我正在尝试测试updateRates()
当我在“发件人”货币字段中输入一个值时是否调用了该方法。
describe('App component', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [FormsModule, HttpClientTestingModule],
declarations: [AppComponent]
}).compileComponents();
}));
describe(':', () => {
let fixture, app;
beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
app = fixture.debugElement.componentInstance;
});
afterEach(() => {
fixture.destroy();
app = null;
});
it('should successfully convert GBP to GBP', fakeAsync(() => {
fixture.detectChanges();
fixture.whenStable().then(() => {
let fromAmountValueEl = fixture.debugElement.query(By.css('#fromAmountValue')).nativeElement;
let toAmountValueEl = fixture.debugElement.query(By.css('#toAmountValue')).nativeElement;
expect(fromAmountValueEl.value).toEqual('0');
expect(toAmountValueEl.value).toEqual('0');
fromAmountValueEl.value = '2';
fromAmountValueEl.dispatchEvent(new Event('input'));
fromAmountValueEl.dispatchEvent(new KeyboardEvent('keyup', {
'key': 'Enter'
}));
spyOn(app, 'updateRates').and.callThrough();
spyOn(app, 'post').and.returnValue(new Observable<any>());
tick();
fixture.detectChanges();
expect(app.updateRates).toHaveBeenCalled();
expect(app.toCurrency).toEqual('GBP');
expect(fromAmountValueEl.value).toEqual('2');
expect(toAmountValueEl.value).toEqual('2');
});
}));
});
});
Stackblitz: https ://stackblitz.com/edit/http-get-post-dgcuur ,并不是说这不包括测试,而是应该让您了解所有内容如何组合在一起。
解决方案
你应该像这样改变你的测试:
describe('AppComponent', () => {
let component: AppComponent;
let fixture: ComponentFixture<AppComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule,
FormsModule
],
declarations: [
AppComponent
]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(AppComponent);
component = fixture.debugElement.componentInstance;
});
it('should successfully convert GBP to GBP', fakeAsync(() => {
fixture.detectChanges();
let fromAmountValueEl = fixture.debugElement.query(By.css('#fromAmountValue'));
let toAmountValueEl = fixture.debugElement.query(By.css('#toAmountValue'));
tick(1000);
expect(fromAmountValueEl.nativeElement.value).toEqual('0');
expect(toAmountValueEl.nativeElement.value).toEqual('0');
component.fromAmount = 2;
fixture.detectChanges();
spyOn(component, 'updateRates').and.callThrough();
spyOn(component, 'post').and.callFake(() => {
component.toAmount = 2;
return new Observable<any>();
});
fromAmountValueEl.triggerEventHandler('keyup', null);
fixture.detectChanges();
tick(1000);
expect(component.updateRates).toHaveBeenCalled();
//expect(component.toCurrency).toEqual('GBP');
expect(fromAmountValueEl.nativeElement.value).toEqual('2');
expect(toAmountValueEl.nativeElement.value).toEqual('2');
}));
it('should successfully convert GBP to EUR', fakeAsync(() => {
fixture.detectChanges();
let fromAmountValueEl = fixture.debugElement.query(By.css('#fromAmountValue'));
let toAmountValueEl = fixture.debugElement.query(By.css('#toAmountValue'));
component.targetCurrency = "EUR";
tick(1000);
expect(fromAmountValueEl.nativeElement.value).toEqual('0');
expect(toAmountValueEl.nativeElement.value).toEqual('0');
component.fromAmount = 2;
fixture.detectChanges();
spyOn(component, 'updateRates').and.callThrough();
spyOn(component, 'post').and.callFake(() => {
return of({ "USD": 2.54, "EUR": 2.24 });
});
fromAmountValueEl.triggerEventHandler('keyup', null);
fixture.detectChanges();
tick(1000);
expect(component.updateRates).toHaveBeenCalled();
//expect(component.toCurrency).toEqual('GBP');
expect(fromAmountValueEl.nativeElement.value).toEqual('2');
expect(toAmountValueEl.nativeElement.value).toEqual('2.24');
})); });
推荐阅读
- c# - System.Core.dll 中出现“System.invalidOperationException”类型的异常,但未在用户代码中处理
- mysql - 优化mysql联合查询
- jmeter - JMeter 5.0 / "JMeterUtils: ERROR! Resource string not found" @ Aggregate Report
- mysql - MySQL - 返回不同的 ID,其中相同 ID 的所有行都没有特定的字段值
- android - Android - 为画布上的复杂路径设置多种颜色
- javascript - 如果以 sessionStorage 结束,如何保持动画状态直到会话结束?
- android - Android 机顶盒(非 AndroidTV)的方向键支持
- jquery - 将动态复选框值保存到数据库
- javascript - 安装quill的源码
- r - 我的数据框中每个组的最大单调增加/减少是多少?