方法调用后发生变化的大理石测试观察值?

Marble testing observable that changes after a method call?

在 Angular 8 中,我有一个带有只读 Observable 属性 的服务,它是从 BehaviorSubject<string> 产生的,其中包含一个描述服务状态的字符串。服务中还有更改服务状态的方法。

export class StateService {
  private _state = new BehaviorSubject<string>('before');

  readonly state$ = this._state.asObservable();

  constructor () { }

  begin() { this._state.next('during'); }

  finish() { this._state.next('after'); }

  reset() { this._state.next('before'); }
}

我想做的是在我的 Jasmine 套件中编写大理石测试,以优雅地测试这个 observable 的值,因为它会发生变化。

let scheduler: TestScheduler;

beforeEach(() => {
  scheduler = new TestScheduler((actual, expected) => {
    expect(actual).toEqual(expected);
  });
});

it('should flow states correctly', () => {
  scheduler.run(({expectObservable, flush}) => {
    const svc = setupSvc(); //Call to a function in the suite that sets up the StateService

    svc.begin();
    svc.finish();
    svc.reset();

    flush();

    expectObservable(svc.state$).toBe('a-b-c-d', {
      a: 'before',
      b: 'during',
      c: 'after',
      d: 'before'
    });
  });
});

我尝试了调用 beginfinishreset 和调度程序的 flush 助手的不同组合,但期望总是只报告第一个价值(a 大理石),没有其他。

我缺少什么才能实现这一目标?还是弹珠测试这个的方法不对?

订阅通过测试助手生成的冷可观察对象似乎有效:

it('should flow states correctly', () => {
  scheduler.run(({ expectObservable, cold }) => {
    const svc = new StateService();

    cold('--a-b-c', {
      a: 'begin',
      b: 'finish',
      c: 'reset'        
    }).subscribe(methodName => {
      svc[methodName]();
    })

    expectObservable(svc.state$).toBe('a-b-c-d', {
      a: 'before',
      b: 'during',
      c: 'after',
      d: 'before'
    });
  });
});

Stackblitz