Jasmine-间谍返回值失败后的预期

Jasmine-间谍返回值失败后的预期,jasmine,karma-jasmine,Jasmine,Karma Jasmine,更改模拟服务方法返回值后,第二个expect语句出现问题。 我尝试过fakeAsync和Jasmine的done,但仍然失败,它只适用于我显然想摆脱的设置超时 不工作: //AppComponent.ts 导出类AppComponent实现OnInit{ 构造函数(私有authService:AppAuthService){} 恩戈尼尼特(){ 此.checkAvailableCalculators(); } 私人支票可用计算器(){ if(this.authService.isLoggedIn

更改模拟服务方法返回值后,第二个
expect
语句出现问题。 我尝试过fakeAsync和Jasmine的done,但仍然失败,它只适用于我显然想摆脱的设置超时

不工作:

//AppComponent.ts
导出类AppComponent实现OnInit{
构造函数(私有authService:AppAuthService){}
恩戈尼尼特(){
此.checkAvailableCalculators();
}
私人支票可用计算器(){
if(this.authService.isLoggedIn()){
this.authService.getAvailableCalculators();
}
this.authService.getSessionEvents().subscribe(e=>{
if(e==SESSION\u EVENTS.login){
this.authService.getAvailableCalculators();
}
});
}
}
//AppComponent.spec.ts
常量authServiceMock={
getSessionEvents:jasmine.createSpy('getSessionEventsSpy')。和.returnValue(of()),
getAvailableCalculators:jasmine.createSpy('getAvailableCalculators'))
};
beforeach(异步(()=>{
TestBed.configureTestingModule({
进口:[],
提供程序:[{提供:AppAuthService,useValue:authServiceMock}],
声明:[AppComponent]
}).compileComponents();
}));
它('应该在登录后检查可用的计算器',fakeAsync(()=>{
组件。ngOnInit();
expect(authServiceMock.getAvailableCalculators).not.toHaveBeenCalled();
authServiceMock.getSessionEvents.and.returnValue(of(SESSION_EVENTS.login));
勾号(1000);
期望(authServiceMock.getAvailableCalculators).toHaveBeenCalled();//{
组件。ngOnInit();
expect(authServiceMock.getAvailableCalculators).not.toHaveBeenCalled();
authServiceMock.getSessionEvents.and.returnValue(of(SESSION_EVENTS.login));

setTimeout(()=>expect(authServiceMock.getAvailableCalculators).tohaveBeenCall(),1000);//我看不到您的组件代码,但在黑暗中拍摄,请尝试以下操作:

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();
    });
  });

//AppComponent.spec.ts
常量authServiceMock={
getSessionEvents:jasmine.createSpy('getSessionEventsSpy'),
getAvailableCalculators:jasmine.createSpy('getAvailableCalculators'))
};
let组件:AppComponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
进口:[],
提供程序:[{提供:AppAuthService,useValue:authServiceMock}],
声明:[AppComponent]
}).compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(AppComponent);
组件=fixture.componentInstance;
});
描述('getSessionEvents返回登录',()=>{
在每个之前(()=>{
//在调用Ngonit之前模拟getSessionEvents
authServiceMock.getSessionEvents.and.returnValue(of(SESSION_EVENTS.login));
fixture.detectChanges();/!!在此fixture.detectChanges之后,ngOnInit
将被调用,无需显式调用。
});
它('登录后应检查可用的计算器',()=>{
expect(authServiceMock.getAvailableCalculators).toHaveBeenCalled();
});
});
描述('getSessionEvents返回您想要的任何其他内容',()=>{
在每个之前(()=>{
authServiceMock.getSessionEvents.and.returnValue(of('anyother'));
fixture.detectChanges();
});
它('不应调用getAvailableCalculators',()=>{
expect(authServiceMock.getAvailableCalculators).not.toHaveBeenCalled();
});
});

运营商可能已经太迟了,无法实现您的实现。
运营商没有推送该值,只需在下次订阅时提供该值。让我知道这是否有效。

您可以分享
authServiceMock的代码吗?我已经用更多的代码更新了我的问题,您可以显示您的合作伙伴的代码吗请使用component?不幸的是,它不起作用。我已经用该组件更新了问题中的代码。我已经编辑了我的答案,希望能有所帮助。问题是,现在你已经更改了逻辑。我想检查一下,订阅时是否正确调用了该方法。你给我的代码将具有与我放入
returnVal时相同的结果ue(of(SESSION_EVENTS.login))
进入
authServiceMock
声明。我认为在每个()之前将其放入
不是一个好的做法
。没错,那样的话,你也会得到同样的结果。也许这不是一个好的做法,取决于你想要什么。请查看我的编辑,我已经将它分为两个
描述
块。一个流可以测试它返回
会话\u事件时会发生什么。登录
,另一个流返回其他内容。