在我的Angular项目中,所有的Rest服务都定义如下,它们有4个签名

public commandsGet(oem: string, countryCode?: string, observe?: 'body', reportProgress?: boolean): Observable<CommandGetResponse>;
    public commandsGet(oem: string, countryCode?: string, observe?: 'response', reportProgress?: boolean): Observable<HttpResponse<CommandGetResponse>>;
    public commandsGet(oem: string, countryCode?: string, observe?: 'events', reportProgress?: boolean): Observable<HttpEvent<CommandGetResponse>>;
    public commandsGet(oem: string, countryCode?: string, observe: any = 'body', reportProgress = false): Observable<any> {

        return this.httpClient.get<CommandGetResponse>(`${this.basePath}/${oem}/commands/`, params);
    }

我正在编写一些测试并试图模拟该服务,但我收到错误消息:"类型为‘Observable’的参数不可赋值给类型为‘Observable;HttpEvent&gt;’的参数."

it('should return expected commands', (done: DoneFn) => {
            const mockResponse: CommandGetResponse  = {
                data: [
                    { commandname: 'A' },
                    { commandname: 'B' }],
                count: 2
            };
           
            spyOn(commandService, 'commandsGet').and.returnValue(of(mockResponse) as Observable<CommandGetResponse>);
            component.ngOnInit();
            fixture.detectChanges();

            expect(component.commands).toEqual(mockResponse.data);
        });

我也试过了


spyOn(commandService, 'commandsGet').and.returnValue(of(mockResponse));

``

Then I'm getting "Argument of type 'Observable<CommandGetResponse>' is not assignable to parameter of type 'Observable<HttpEvent<CommandGetResponse>>'.

当我定义:

const httpResponse = new HttpResponse({ body: mockResponse });
spyOn(commandService, 'commandsGet').and.returnValue(of(httpResponse));

我没有收到任何错误,但我的测试失败,因为返回的对象不是"CommandGetResponse"类型

这方面的任何帮助都将是受欢迎的.

谢谢

推荐答案

对于测试文件来说,设置什么类型并不重要,所以使用any是安全的,只需专注于测试和覆盖!

    it('should return expected commands', (done: DoneFn) => {
        const mockResponse: any = { // <- changed here!
            data: [
                { commandname: 'A' },
                { commandname: 'B' }],
            count: 2
        };
       
        spyOn<any>(commandService, 'commandsGet').and.returnValue(of(mockResponse) as any);// <- changed here!
        component.ngOnInit();
        fixture.detectChanges();

        expect(component.commands).toEqual(mockResponse.data);
    });

Angular相关问答推荐

Angular 16路卫单元测试可观察到的SpyObj属性

尽管有模拟间谍实现,规范文件仍调用真正的服务

[已解决]如何在模块ngDoBootstrap中测试Angular 自定义元素和做覆盖

独立Angular 零部件不在辅零部件或共享零部件中工作

无效的ICU消息.缺少';}';|Angular material 拖放(&A)

[NullInjectorError]:R3InjectorError(Standalone[_AppComponent])[]:NullInjectorError:No provider for _HttpClient

更改动态表单控制Angular 的值

从canActivate创建时,Angular material 对话框不接收MAT_DIALOG_DATA

Angular 信号 - 使用 mutate() 与使用 forEach() react 性

Angular Signals 如何影响 RXJS Observables 以及在这种情况下变化检测会发生什么

Angular:为什么在 ngAfterContentInit 之后变量未定义?

如何在Angular中自动路由到子路由

我想将一个对象传递给一个子组件.我做了一切,但没有任何效果. (Angular )

如何检测滚动到html元素的底部

Angular 5 Mat-grid 列表响应式

如何从Angular 2 中的按钮单击触发输入文件的单击事件?

如何在 Angular 2 中正确设置 Http 请求标头

如何组合两个可观察到的结果?

Angular 2 可用的 yeoman 生成器

Angular 9 引入了需要加载的全局$localize()函数