mardi 17 mars 2020

Angular testing - How to test an observable chain where two services are called?

I am trying to test an observable chain where 2 services are called. I am trying to test if the both services are called exactly one time. Since both services return observables I use mergeMap to chain the calls as you can see bellow:

app.component.ts

public aliceInChain(): void {
    this.serviceA
      .doSomething()
      .pipe(
        mergeMap(() => {
          return this.serviceB.doSomethingElse();
        })
      )
      .subscribe(r => {
        console.log(r);
      });
  }

In my spec file I have:

describe("AppComponent", () => {
  let fixture;
  let component;

  beforeEach(async(() => {
    const serviceASpy = jasmine.createSpyObj("serviceA", {
      bravo: of()
    });

    const serviceBSpy = jasmine.createSpyObj("serviceB", {
      eatSushi: of()
    });

    TestBed.configureTestingModule({
      imports: [FormsModule],
      declarations: [AppComponent],
      providers: [
        { provide: ServiceA, useValue: serviceASpy },
        { provide: ServiceB, useValue: serviceBSpy }
      ]
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(AppComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });



  fit("should call serviceA and serviceB", done => {

    component.aliceInChain();
    expect(component.serviceA.doSomething).toHaveBeenCalledTimes(1);


  });
});

I can make sure that the first service was called but how can I test that the second was aswell? Is this possible being the method being tested void?

Aucun commentaire:

Enregistrer un commentaire