我的组件订阅了通过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: [...]
});
});当我进行测试时,我得到:
错误:没有初始化测试调度程序
通过搜索,我尝试了setting my compile target to ES5
我现在也在使用最新版本的茉莉花弹珠: 0.6.0
我做错了什么?
发布于 2020-05-22 15:22:48
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()
});
});发布于 2020-04-02 02:34:39
我想我以前也曾面对过这个问题。对于angular-spectator,我不太确定,但是对于jasmine,在第一个beforeEach上,我称之为initTestScheduler和addMatchers。
就像这样:
import { addMatchers, initTestScheduler } from 'jasmine-marbles';
describe('MyComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
....
}).compileComponents();
initTestScheduler();
addMatchers();
}));
});发布于 2021-03-19 12:57:28
这对我起了作用,相关的位是提供者数组(剩下的代码只留给上下文):
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [BusinessModule, RouterTestingModule, HttpClientTestingModule, ToastrModule.forRoot()],
providers: [
{
provide: DataSourcesService,
useValue: {
activeBusinessDataSources$: cold('--x|', { x: activeBusinessDataSources })
}
}
]
}).compileComponents();
});https://stackoverflow.com/questions/60980618
复制相似问题