茉莉花-间谍返回值失败后期望

时间:2020-02-26 11:41:20

标签: jasmine karma-jasmine

更改模拟服务方法的返回值后,第二条expect语句出现问题。 我已经尝试过fakeAsync和Jasmine的done,但仍然失败,它仅适用于显然很想摆脱的setTimeout

不起作用:

// AppComponent.ts
export class AppComponent implements OnInit {
  constructor(private authService: AppAuthService) {}

  ngOnInit() {
    this.checkAvailableCalculators();
  }

  private checkAvailableCalculators() {
    if (this.authService.isLoggedIn()) {
      this.authService.getAvailableCalculators();
    }

    this.authService.getSessionEvents().subscribe(e => {
      if (e === SESSION_EVENTS.login) {
        this.authService.getAvailableCalculators();
      }
    });
  }
}

// AppComponent.spec.ts
    const authServiceMock = {
      getSessionEvents: jasmine.createSpy('getSessionEventsSpy').and.returnValue(of()),
      getAvailableCalculators: jasmine.createSpy('getAvailableCalculators')
    };

    beforeEach(async(() => {
      TestBed.configureTestingModule({
        imports: [],
        providers: [{provide: AppAuthService, useValue: authServiceMock}],
        declarations: [AppComponent]
      }).compileComponents();
    }));

    it('should check for available calculators after login', fakeAsync(() => {
      component.ngOnInit();

      expect(authServiceMock.getAvailableCalculators).not.toHaveBeenCalled();

      authServiceMock.getSessionEvents.and.returnValue(of(SESSION_EVENTS.login));
      tick(1000);

      expect(authServiceMock.getAvailableCalculators).toHaveBeenCalled(); // <=== FAILS
    }));

工作:


    it('should check for available calculators after login', () => {
      component.ngOnInit();

      expect(authServiceMock.getAvailableCalculators).not.toHaveBeenCalled();

      authServiceMock.getSessionEvents.and.returnValue(of(SESSION_EVENTS.login));

      setTimeout(() => expect(authServiceMock.getAvailableCalculators).toHaveBeenCalled(), 1000); // <=== PASSES
    });

1 个答案:

答案 0 :(得分:0)

我看不到您的组件代码,但是在黑暗中拍照,请尝试以下操作:

fixture.whenStable()等到诺言完成。

it('should check for available calculators after login', async(done) => {
      component.ngOnInit();

      expect(authServiceMock.getAvailableCalculators).not.toHaveBeenCalled();

      authServiceMock.getSessionEvents.and.returnValue(of(SESSION_EVENTS.login));
      await fixture.whenStable();
      expect(authServiceMock.getAvailableCalculators).toHaveBeenCalled(); 
      done();
    });

================================================ ===编辑==============

立即尝试:

// AppComponent.spec.ts
    const authServiceMock = {
      getSessionEvents: jasmine.createSpy('getSessionEventsSpy'),
      getAvailableCalculators: jasmine.createSpy('getAvailableCalculators')
    };
    let component: AppComponent;
    let fixture: ComponentFixture<AppComponent>;

    beforeEach(async(() => {
      TestBed.configureTestingModule({
        imports: [],
        providers: [{provide: AppAuthService, useValue: authServiceMock}],
        declarations: [AppComponent]
      }).compileComponents();
    }));

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

   describe('getSessionEvents returning login', () => {
     beforeEach(() => {
      // mock getSessionEvents before ngOnInit is called
      authServiceMock.getSessionEvents.and.returnValue(of(SESSION_EVENTS.login));
      fixture.detectChanges(); // !! After this fixture.detectChanges, ngOnInit 
      will be called, no need to call it explicitly.
     });

     it('should check for available calculators after login', () => {
      expect(authServiceMock.getAvailableCalculators).toHaveBeenCalled(); 
    });
   });

  describe('getSessionEvents returning anything else you want', () => {
    beforeEach(() => {
      authServiceMock.getSessionEvents.and.returnValue(of('anything else'));
      fixture.detectChanges();
    });

    it('should not call getAvailableCalculators', () => {
      expect(authServiceMock.getAvailableCalculators).not.toHaveBeenCalled();
    });
  });

of运算符可能对您实施的实施为时已晚。 of运算符不推送值,仅在下一个订阅中提供此值。让我知道这是否有效。