4

我的组件订阅了服务中的 Observable,该服务通过 Ngrx 选择器填充,为简洁起见在此概括:

export class MyService {
  signInFalied$: Observable<boolean>;

  constructor(
    private store: Store<MyAppState>,
  ) {
    this.signInFailed$ = this.store.select(mySelectors.signInFailed);
  }
}

我的组件具有基于此状态值的条件内容,我想测试是否显示了正确的内容。在我的测试中,我为服务提供了一个模拟,如下所示:

describe('My Test', () => {
  let spectator: SpectatorHost<MyComponent>;

  const createHost = createHostComponentFactory({
    component: MyComponent,
    declarations: [MyComponent],
    providers: [
      ...,
      mockProvider(MyService, {
        signInFailed$: cold('x', { x: null }),
        ...
      }),
    ],
    imports: [...]
  });
});

当我运行测试时,我得到:

错误:未初始化测试调度程序

通过搜索,我尝试将编译目标设置为 ES5

我此时也在使用最新版本的 jasmine-marbles:0.6.0

我究竟做错了什么?

4

3 回答 3

3

cold需要在一个async范围内。因此,您需要添加 a并在范围内beforeEach调用它:async

import { async } from '@angular/core/testing';

describe('My Test', () => {
   beforeEach(async(() => {

       TestBed.configureTestingModule({
           providers: [
              ...,
              mockProvider(MyService, {
                signInFailed$: cold('x', { x: null }),
                ...
              }),
            ],
        })
        .compileComponents()
   });


});
于 2020-05-22T15:22:48.457 回答
0

这对我有用,相关位是提供程序数组(仅将其余代码留作上下文):

beforeEach(async () => {
  await TestBed.configureTestingModule({
    imports: [BusinessModule, RouterTestingModule, HttpClientTestingModule, ToastrModule.forRoot()],
    providers: [
      {
        provide: DataSourcesService,
        useValue: {
          activeBusinessDataSources$: cold('--x|', { x: activeBusinessDataSources })
        }
      }
    ]
  }).compileComponents();
});
于 2021-03-19T12:57:28.540 回答
0

我想我以前遇到过这个问题。我不确定,angular-spectatorjasmine在我第一次beforeEach打电话initTestScheduleraddMatchers.

像这样的东西:

import { addMatchers, initTestScheduler } from 'jasmine-marbles';

describe('MyComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({ 
     ....
    }).compileComponents();

    initTestScheduler();
    addMatchers();
  }));
});
于 2020-04-02T02:34:39.943 回答