我的组件订阅了服务中的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
我现在也在使用最新版本的茉莉花弹珠:0.6.0
我做错了什么?
cold
需要在async
作用域中。因此,您需要添加一个beforeEach
,并在async
作用域中调用它:
import { async } from '@angular/core/testing';
describe('My Test', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [
...,
mockProvider(MyService, {
signInFailed$: cold('x', { x: null }),
...
}),
],
})
.compileComponents()
});
});
我想我以前遇到过这个问题。我不确定angular-spectator
,但对于第一个beforeEach
上的jasmine
,我调用initTestScheduler
和addMatchers
。
类似这样的东西:
import { addMatchers, initTestScheduler } from 'jasmine-marbles';
describe('MyComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
....
}).compileComponents();
initTestScheduler();
addMatchers();
}));
});
这对我来说很有效,相关的位是providers数组(剩下的代码只用于上下文(:
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [BusinessModule, RouterTestingModule, HttpClientTestingModule, ToastrModule.forRoot()],
providers: [
{
provide: DataSourcesService,
useValue: {
activeBusinessDataSources$: cold('--x|', { x: activeBusinessDataSources })
}
}
]
}).compileComponents();
});