简体   繁体   English

使用Karma / Jasmin在Angular 4中测试使用.next,.subscribe的简单服务

[英]Testing a simple service that uses .next, .subscribe in Angular 4 using Karma / Jasmin

I have a simple service in my app like so... 我的应用程式中有一项简单的服务,就像这样...

import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {Subject} from 'rxjs/Subject';

@Injectable()
export class WizardDialogNavigationService {

  public navAction$: Observable<any>;
  private navActionSubject: Subject<any> = new Subject();

  constructor() {
    this.navAction$ = this.navActionSubject.asObservable();
  }

  public navAction(action: string): void {
    this.navActionSubject.next(action);
  }
}

It's not the most complex service, it just allows me to communicate between components. 它不是最复杂的服务,它只允许我在组件之间进行通信。 Now I need to test it, I have to admit I know very little about testing. 现在我需要测试,我不得不承认我对测试了解甚少。 I just want to check that when WizardDialogNavigationService.navAction is called the navAction$ Observable contains the passed string, so I wrote the following... 我只是想检查,当WizardDialogNavigationService.navAction被称为navAction$可观察包含传递的字符串,所以我写了下面...

import {TestBed, inject, async} from '@angular/core/testing';
import {Observable} from 'rxjs/Rx';
import {expect} from 'chai';
import * as sinon from 'sinon';
import {WizardDialogNavigationService} from './wizard-dialog-navigation.service';

describe('WizardDialogNavigationService', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [WizardDialogNavigationService]
    });
  })

  describe('navAction$ Observable', () => {

    let sut: WizardDialogNavigationService; // sut = Service Under Test

    beforeEach(inject([WizardDialogNavigationService], (service: WizardDialogNavigationService) => {
      sut = service;
    }));

    it('should be the last passed value', async(() => {
      const resultList = [];
      sut.navAction('TestString');

      sut.navAction$.subscribe((result) => resultList.push(result));
      console.log(resultList); // output is []
      expect(resultList.indexOf('TestString')).to.equal(0);
    }));
  });
});

I think I am going about this wrong as the resultList array doesn't contain the passed value? 我想我要解决这个错误,因为resultList数组不包含传递的值? It's as if the .next() .subscribe() isn't being called / ran. 好像.next() .subscribe()未被调用/运行。 If anyone can tell me where I am going wrong I would appreciate it. 如果有人能告诉我我要去哪里错,我将不胜感激。

You need a BehaviorSubject which will return the already stored value, if you subscribe to it directly. 如果直接subscribe ,则需要一个BehaviorSubject ,它将返回已存储的值。

So you can change the Subject to a BehaviorSubject and subscribe to it and it will work. 因此,您可以将Subject更改为BehaviorSubject并订阅它,它将起作用。

Doc: 文件:

https://github.com/ReactiveX/rxjs/blob/master/doc/subject.md#behaviorsubject https://github.com/ReactiveX/rxjs/blob/master/doc/subject.md#behaviorsubject

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

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