Я не вижу код вашего компонента, но делаю снимок в темноте, попробуйте следующее:
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
не выводит значение sh, просто укажите это значение при следующей подписке. Дайте мне знать, если это работает.