dimanche 29 octobre 2017

Angular 2+ testing: fakeAsync with Inject

I am trying to test service with private method which is called in constructor and contains observable:

import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/interval';

@Injectable()
export class SomeService {

  private booleanValue: boolean = false;

  constructor() {
    this.obsMethod();
  }

  private obsMethod() {
    Observable.interval(5000).subscribe(() => {
      this.booleanValue = !this.booleanValue;
    });
  }

  public getBooleanValue() {
    return this.booleanValue;
  }
}

I prepared three specs. First with simple instance of service created with new operator. And it works. Second with TestBed.get() injection. And it works too.

When I use inject in beforeEach spec does not work. But why? Is it problem with fakeAsync and inject used at the same time? How can I use them both together?

I created working demo on plunker with service and three specs.

http://ift.tt/2z0W0bL

Aucun commentaire:

Enregistrer un commentaire