【发布时间】:2022-01-16 11:45:10
【问题描述】:
在我的 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()
);
}
在我的规范文件中:
describe('PostListComponent', () => {
let component: PostListComponent;
let fixture: ComponentFixture<PostListComponent>;
let debugElement: DebugElement;
beforeEach(async () => {
await TestBed.configureTestingModule({
// ...
})
.compileComponents()
.then(() => {
fixture = TestBed.createComponent(PostListComponent);
component = fixture.componentInstance;
debugElement = fixture.debugElement;
fixture.detectChanges();
});
});
describe('functions', () => {
it('should be delete with proxy.delete() and reload datas', fakeAsync(() => {
spyOn(component, 'loadDatas');
component.delete(1);
flush();
expect(component.loadDatas).toHaveBeenCalledTimes(1);
}));
});
});
在实际环境中switchMap 工作正常,但在测试中失败并显示以下消息:
PostListComponent > functions > should be delete with proxy.delete() and reload datas
Expected spy loadDatas to have been called once. It was called 0 times.
为什么我的测试中没有运行switchMap?有什么想法吗?
【问题讨论】:
-
这个答案不能解决你的问题吗? stackoverflow.com/questions/49910827/…
-
@S.Hashiba 不,我在提问之前试过了
标签: angular rxjs jasmine angular-test switchmap