简体   繁体   English

使用keycloack进行角度测试“用户未登录”

[英]Angular testing with keycloack “user is not logged in”

I have a small application at the moment that uses Keycloak to have a sso. 我现在有一个小应用程序使用Keycloak来获得一个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: 但是,当我想测试我的main.component.ts时,我遇到标准'它应该创建'测试失败的问题,我收到以下错误消息:

MainComponent should create Error: User not logged in MainComponent应该创建错误:用户未登录

Below is my main.component.spec file 下面是我的main.component.spec文件

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

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

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

beforeEach(async(() => { 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(); }); 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(); }); // TODO:期待用户登录但不会发生 - >与KeyCloak有关('应该创建',()=> {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! 谢谢!

Yes, you should mock service: 是的,你应该模拟服务:

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

// in TestBed.configureTestingModule providers: [ { provide: KeycloakService, useValu: Mock1 } ] //在TestBed.configureTestingModule提供程序中:[{provide:KeycloakService,useValu:Mock1}]

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

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