简体   繁体   English

在 subscribe 中使用 expect 时,spec 没有期望

[英]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:我正在尝试创建一个测试来验证从我的服务中的 Observable 返回的值:

  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 SPEC HAS NO EXPECTATIONS 值切换时应包含真实值

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.我试过使用fakeAsync()Flush()来清除任务队列,我试过使用Done() function 将任何异步任务标记为已完成但似乎没有任何效果。

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.如果这不起作用,请尝试删除skip(1)以查看它是否会为您提供有关为什么它不起作用的任何其他提示。

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

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