尝试使用 Angular TestBed 时出现奇怪的 RxJS 错误

Weird RxJS error when trying to use Angular TestBed

我正在尝试在我的 Angular 项目中实施单元测试,但我 运行 收到一条错误消息,我不知道如何处理。

这是我的测试文件的代码:

import {AuthComponent} from './auth.component';
import {ConditionalMerchandising} from '../../common/merchandising/conditionalmerchandising.service';
import {TestBed} from '@angular/core/testing';
import {RouterTestingModule} from '@angular/router/testing';
import {of} from 'rxjs';

describe('Auth base page', () => {

    let _cm: jasmine.SpyObj<ConditionalMerchandising>;

    beforeEach(() => {
        _cm = jasmine.createSpyObj('ConditionalMerchandising', [ 'getMerchandising' ]);

        TestBed.configureTestingModule({
            declarations: [ AuthComponent ],
            providers:    [ {provide: ConditionalMerchandising, useValue: _cm } ],
            imports:      [ RouterTestingModule ]
        }).compileComponents();
    });

    it ('loads the picture', () => {
        TestBed.get(ConditionalMerchandising).getMerchandising.and.returnValue(of(''));

        const fixture = TestBed.createComponent(AuthComponent);
        fixture.detectChanges();

        console.log(fixture.nativeElement.querySelector('.page-contents'));

    });

});

当我 运行 它时,我得到这个奇怪的错误:

ERROR in src/app/web-ui/auth/auth.component.ts(24,17): error TS2345: Argument of type 'import("C:/Users/Bart/workspace/epicuro-web/node_modules/rxjs/internal/types").OperatorFunction<any, any>' is not assignable to parameter of type 'import("C:/Users/Bart/workspace/epicuro-web/src/app/common/node_modules/rxjs/internal/types").OperatorFunction<any, any>'.
  Types of parameters 'source' and 'source' are incompatible.
    Type 'import("C:/Users/Bart/workspace/epicuro-web/src/app/common/node_modules/rxjs/internal/Observable").Observable<any>' is not assignable to type 'import("C:/Users/Bart/workspace/epicuro-web/node_modules/rxjs/internal/Observable").Observable<any>'.
      Types of property 'operator' are incompatible.
        Type 'import("C:/Users/Bart/workspace/epicuro-web/src/app/common/node_modules/rxjs/internal/Operator").Operator<any, any>' is not assignable to type 'import("C:/Users/Bart/workspace/epicuro-web/node_modules/rxjs/internal/Operator").Operator<any, any>'.
          Types of property 'call' are incompatible.
            Type '(subscriber: import("C:/Users/Bart/workspace/epicuro-web/src/app/common/node_modules/rxjs/internal/Subscriber").Subscriber<any>, source: any) => import("C:/Users/Bart/workspace/epicuro-web/src/app/common/node_modules/rxjs/internal/types").TeardownLogic' is not assignable to type '(subscriber: import("C:/Users/Bart/workspace/epicuro-web/node_modules/rxjs/internal/Subscriber").Subscriber<any>, source: any) => import("C:/Users/Bart/workspace/epicuro-web/node_modules/rxjs/internal/types").TeardownLogic'.
              Types of parameters 'subscriber' and 'subscriber' are incompatible.
                Property '_parentOrParents' is missing in type 'Subscriber<any>' but required in type 'Subscriber<any>'.

它抱怨的线路是 const fixture = TestBed.createComponent(AuthComponent);

我哪里做错了?

看起来像可观察值 return 的类型不匹配。试试这个:

TestBed.get(ConditionalMerchandising).getMerchandising.and.returnValue(of({}));