Ngrx 效果规范抛出错误为“未初始化测试调度程序"; [英] Ngrx Effects spec throws Error as "No test scheduler initialized"
问题描述
尝试使用现有的和最近迁移的 Angular 7 项目运行简单的效果测试.但我收到如下错误.
Trying to run a simple effects test with an existing and recently migrated Angular 7 project. But I get error as below.
错误:未初始化测试调度程序在 getTestScheduler (node_modules/jasmine-marbles/es6/src/scheduler.js:11:1)在新的 TestHotObservable (node_modules/jasmine-marbles/es6/src/test-observables.js:21:39)在 Module.hot (node_modules/jasmine-marbles/es6/index.js:7:1)
Error: No test scheduler initialized at getTestScheduler (node_modules/jasmine-marbles/es6/src/scheduler.js:11:1) at new TestHotObservable (node_modules/jasmine-marbles/es6/src/test-observables.js:21:39) at Module.hot (node_modules/jasmine-marbles/es6/index.js:7:1)
我在效果规范文件中的代码是对 jasmine-marbles 的基本标准检查.
My code in effects spec file is basic standard check with 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);
有没有人见过并解决过这个错误?
Has anyone seen and resolved this error before?
推荐答案
虽然改用 ES5 解决了问题,但我的同事提出了更好的解决方案.解决方案是在 src/test.ts 文件中添加以下几行.我更喜欢它,因为它允许在 ES6 中继续测试.
Although changing to ES5 fixed the problem, my colleague has come up with a better solution. Solution is to add the following lines in src/test.ts file. I like it better as it allows to continue testing in 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();
});
这篇关于Ngrx 效果规范抛出错误为“未初始化测试调度程序";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!