4
votes

I have a small application at the moment that uses Keycloak to have a sso. However when I want to ng test my main.component.ts I run in to the problem that the standard 'it should create' test fails, I get the following error message:

MainComponent should create Error: User not logged in

Below is my main.component.spec file

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { MainComponent } from './main.component'; import {AppTopBarComponent} from '../app.topbar.component'; import {AppMenuComponent} from '../app.menu.component'; import {RouterModule} from '@angular/router'; import {RouterTestingModule} from '@angular/router/testing'; import {FormsModule} from '@angular/forms'; import {MenuModule} from 'primeng/primeng'; import {AppSubMenuComponent} from '../app.menu.component'; import {KeycloakService} from 'keycloak-angular';

describe('MainComponent', () => { let component: MainComponent;
let fixture: ComponentFixture;

beforeEach(async(() => {

TestBed.configureTestingModule({
  declarations: [ MainComponent, AppTopBarComponent, AppMenuComponent, AppSubMenuComponent],
  providers: [KeycloakService],
  imports: [RouterModule, RouterTestingModule, FormsModule, MenuModule]
}).compileComponents();   }));

beforeEach(() => { fixture = TestBed.createComponent(MainComponent); component = fixture.componentInstance; fixture.detectChanges(); }); // TODO: Expects a user to log in but does not happen --> Has something to do with KeyCloak it('should create', () => { expect(component).toBeTruthy(); });

});

I don't have much experience with testing so I'm not sure how I should approach this? Mock the keycloak somehow?

thanks!

1

1 Answers

1
votes

Yes, you should mock service:

 let Mock1 ={
   someMethod: jasmine.createSpy('someMethod');
  };

// in TestBed.configureTestingModule providers: [ { provide: KeycloakService, useValu: Mock1 } ]