[英]Error: No test scheduler initialized when using jasmine-marbles with Spectator in Angular test
My component subscribes to an Observable in a Service, which is populated via an Ngrx selector, generalized here for brevity:我的组件订阅了一个服务中的 Observable,它通过一个 Ngrx 选择器填充,为了简洁起见,这里概括为:
export class MyService {
signInFalied$: Observable<boolean>;
constructor(
private store: Store<MyAppState>,
) {
this.signInFailed$ = this.store.select(mySelectors.signInFailed);
}
}
My component has conditional content based on this state value, and I would like to test that the correct content is displayed.我的组件具有基于此状态值的条件内容,我想测试是否显示了正确的内容。 In my test, I'm providing a mock for the service as such:在我的测试中,我为该服务提供了一个模拟:
describe('My Test', () => {
let spectator: SpectatorHost<MyComponent>;
const createHost = createHostComponentFactory({
component: MyComponent,
declarations: [MyComponent],
providers: [
...,
mockProvider(MyService, {
signInFailed$: cold('x', { x: null }),
...
}),
],
imports: [...]
});
});
When I run tests, I get:当我运行测试时,我得到:
Error: No test scheduler initialized错误:未初始化测试调度程序
Through searches I have tried setting my compile target to ES5通过搜索,我尝试将编译目标设置为 ES5
I'm also using the latest version of jasmine-marbles at this time: 0.6.0我此时也在使用最新版本的 jasmine-marbles:0.6.0
What am I doing wrong?我究竟做错了什么?
cold
needs to be in an async
scope. cold
需要在async
范围内。 So you need to add a beforeEach
and call this in an async
scope:所以你需要添加一个beforeEach
并在async
范围内调用它:
import { async } from '@angular/core/testing';
describe('My Test', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [
...,
mockProvider(MyService, {
signInFailed$: cold('x', { x: null }),
...
}),
],
})
.compileComponents()
});
});
I think I have faced this issue before.我想我以前遇到过这个问题。 I am not sure about angular-spectator
but for jasmine
on the first beforeEach
I call initTestScheduler
and addMatchers
.我不确定angular-spectator
但对于jasmine
在第一个beforeEach
我调用initTestScheduler
和addMatchers
。
Something like this:像这样的东西:
import { addMatchers, initTestScheduler } from 'jasmine-marbles';
describe('MyComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
....
}).compileComponents();
initTestScheduler();
addMatchers();
}));
});
This worked for me, the relevant bit being the providers array (have left the rest of the code for context only):这对我有用,相关位是提供者数组(仅将其余代码留给上下文):
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [BusinessModule, RouterTestingModule, HttpClientTestingModule, ToastrModule.forRoot()],
providers: [
{
provide: DataSourcesService,
useValue: {
activeBusinessDataSources$: cold('--x|', { x: activeBusinessDataSources })
}
}
]
}).compileComponents();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.