Home > Net >  spec has no expectations when using expect within subscribe
spec has no expectations when using expect within subscribe

Time:01-31

I am trying to create a test to validate the value returned from an Observable within my service:

  private userIsRegisteringToggle = new Subject<void>();

  userIsRegistering$: Observable<boolean> = this.userIsRegisteringToggle.asObservable().pipe(
    scan(previous => !previous, false),
    startWith(false)
  );

 toggleUserIsRegistering() {
   this.userIsRegisteringToggle.next();
  }

This is the test:

 it('should contain true value when value is toggled', fakeAsync((done: DoneFn) => {
      service.toggleUserIsRegistering();
      flush();
      service.userIsRegistering$.pipe(skip(1))
      .subscribe(value => {
          expect(value).toEqual(true);
          done();
      });
 }));

The test keeps returning this:

SPEC HAS NO EXPECTATIONS should contain true value when value is toggled

The test itself passes however.

I've tried using fakeAsync() with Flush() to clear the task queue and I've tried using the Done() function to mark any async tasks as complete but nothing seems to be working.

Any ideas?

CodePudding user response:

I think this is an issue of late subscribing with a subject. Try to subscribe first and then do the actions.

 it('should contain true value when value is toggled', fakeAsync((done: DoneFn) => {
      service.userIsRegistering$.pipe(skip(1))
      .subscribe(value => {
          expect(value).toEqual(true);
          done();
      });
      service.toggleUserIsRegistering();
      flush();
 }));

If that doesn't work, try removing the skip(1) to see if it gives you any other hints as to why it doesn't work.

  • Related