繁体   English   中英

Ngrx 效果规范抛出错误为“未初始化测试调度程序”

[英]Ngrx Effects spec throws Error as "No test scheduler initialized"

尝试使用现有的和最近迁移的 Angular 7 项目运行简单的效果测试。 但我得到如下错误。

错误:在新的 TestHotObservable (node_modules/jasmine-marbles/es6/src/test-observables.js:21:39) 的 getTestScheduler (node_modules/jasmine-marbles/es6/src/scheduler.js:11:1) 上没有初始化测试调度程序) 在 Module.hot (node_modules/jasmine-marbles/es6/index.js:7:1)

我的效果规范文件中的代码是对 jasmine-marbles 的基本标准检查。

const action = new Load(request);
const completion = new LoadSuccess(result);

actions$ = hot('-a-', { a: action});
const response = cold('-a|', {a: result});
const expected = cold('--b', {b: completion});
service.getSomething.and.returnValue(result);
expect(effects.load$).toBeObservable(expected);

有没有人见过并解决过这个错误?

虽然改用ES5解决了这个问题,但我的同事提出了一个更好的解决方案。 解决方案是在src / test.ts文件中添加以下行。 我更喜欢它,因为它允许继续在ES6中进行测试。

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

// configure matchers for jasmine-marbles
jasmine.getEnv().beforeAll(() => {
  return addMatchers();
});
jasmine.getEnv().beforeEach(() => {
 initTestScheduler();
});
jasmine.getEnv().afterEach(() => {
 getTestScheduler().flush();
 resetTestScheduler();
});

将jasmine-marbles升级到0.6.0为我解决了这个问题。

经过进一步的研究发现,这是由于tsconfig.spec.json中的编译器选项。 最初它设置为“目标”:“es6”,将其更改为es5修复了此问题,规格现已成功运行。

在我的情况下, No test scheduler initialized错误的原因是在beforeEach块之外使用cold

beforeEach块中创建测试可观察beforeEach解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM