大理石测试可观察到方法调用后发生了变化?

编程入门 行业动态 更新时间:2024-10-22 15:26:00
本文介绍了大理石测试可观察到方法调用后发生了变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在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套件中编写大理石测试,以优雅地测试此可观察值的价值,因为它会发生变化.

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' }); }); });

我尝试了调用begin,finish,reset和调度程序的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 In Angular 8, I have a service with a readonly Observable property, spawned from a BehaviorSubject<string> that contains a string describing the service's state. Also in the service are methods that change the state of the service.

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'); } }

What I want to do is write marble tests in my Jasmine suite to elegantly test the value of this observable as it would change.

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' }); }); });

I've tried varying combinations of calling begin, finish, reset, and the scheduler's flush helper, but the expectation always reports just the one initial value (the a marble) and no others.

What am I missing to be able to pull this off? Or are marbles the wrong way to go about testing this?

解决方案

Subscribing to a cold observable produced via the test helper seems to work:

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

更多推荐

大理石测试可观察到方法调用后发生了变化?

本文发布于:2023-10-31 07:31:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545442.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大理石   发生了   测试   方法   观察到

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!