gpt4 book ai didi

使用 keycloak "user is not logged in"进行 Angular Testing

转载 作者:太空狗 更新时间:2023-10-29 19:26:42 26 4
gpt4 key购买 nike

我目前有一个使用 Keycloak 进行 SSO 的小应用程序。但是,当我想对我的 main.component.ts 进行 ng 测试时,我遇到了标准“它应该创建”测试失败的问题,我收到以下错误消息:

MainComponent 应该创建错误:用户未登录

下面是我的 main.component.spec 文件

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

});

我没有太多测试经验,所以我不确定应该如何处理这个问题?以某种方式模拟 key 斗篷?

谢谢!

最佳答案

是的,你应该模拟服务:

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

//在 TestBed.configureTestingModule 中 供应商:[{提供:KeycloakService,useValu:Mock1}]

关于使用 keycloak "user is not logged in"进行 Angular Testing ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49173685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com