简体   繁体   中英

spec has no expectations when using expect within subscribe

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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