在我的 Angular 組件中,我有以下代碼:
delete(post: PostInterface): void {
const delete$ = this.appDataService.proxy
.delete(post, this.paginate)
.pipe(switchMap(() => this.loadDatas()));
this.subscriptions.add(
this.appDataService.start(delete$, 'delete').subscribe()
);
}
我有這個測驗:
beforeEach(async () => {
loadDatasSpy = spyOn(component, 'loadDatas').and.returnValue(of(paginate));
});
it('should call proxy.delete with model on call delete', fakeAsync(() => {
loadDatasSpy.calls.reset();
const id = 1;
const post: PostInterface = new Post().init({id});
const proxySpy = jasmine.createSpyObj('ProxyService', ['delete']);
proxySpy.delete.and.returnValue(of(id));
const appDataServiceSpy = jasmine.createSpyObj('AppDataService', ['proxy', 'start']);
appDataServiceSpy.start.and.returnValue(of(id));
appDataServiceSpy.proxy = proxySpy;
component.appDataService = appDataServiceSpy;
component.delete(post);
flush();
// ?? this expectation is passes
expect(component.appDataService.proxy.delete)
.withContext('appDataService.proxy.delete')
.toHaveBeenCalledTimes(1);
// ?? this expectation is passes
expect(component.appDataService.proxy.delete)
.withContext('appDataService.proxy.delete')
.toHaveBeenCalledWith(post, component.paginate);
// ?? this expectation is passes
expect(component.appDataService.start)
.withContext('appDataService.start')
.toHaveBeenCalledTimes(1);
// ? this expectation is fail
expect(component.loadDatas).toHaveBeenCalledTimes(1);
}));
最后一個期望失敗并顯示此訊息:
Expected spy loadDatas to have been called once. It was called 0 times.
我認為是switchMap問題的原因,但我不知道為什么。我嘗試在上面的測驗代碼上應用flushMicrotasks(),tick()并結合fixture.whenStabel()沒有成功。
知道如何解決這個問題嗎?
我也將不勝感激。
uj5u.com熱心網友回復:
您必須使用 done() 而不是 fakeAsync。fakeAsync 將等待,但訂閱不會完成。done() 函式將“觸發” rxjs 運算子,您可以在訂閱者的訂閱中測驗結果。
我想就像這樣:
it('should call proxy.delete with model on call delete', (done) => {
...
component.delete(post);
component.appDataService.start.subscribe(() => {
expect(component.appDataService.proxy.delete)
.withContext('appDataService.proxy.delete')
.toHaveBeenCalledTimes(1);
...
expect(component.loadDatas).toHaveBeenCalledTimes(1);
}
done();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/475369.html
上一篇:涉及隨機性的演算法的TDD
下一篇:任務':vision-camera-code-scanner:checkDebugAndroidTestAarMetadata'執行失敗
