我现在有一个小应用程序使用Keycloak来获得sso . 但是,当我想测试我的main.component.ts时,我遇到标准'它应该创建'测试失败的问题,我收到以下错误消息:
MainComponent应该创建错误:用户未登录
下面是我的main.component.spec文件
来自'@ angular / core / testing'的import {async,ComponentFixture,TestBed};从'./main.component'导入;从'../app.topbar.component'导入;从'../app.menu.component'导入;从'@ angular / router'导入;从'@ angular / router / testing'导入;从'@ angular / forms'导入;从'primeng / primeng'导入;从'../app.menu.component'导入;从'keycloak-angular'导入; describe('MainComponent',()=> {let component:MainComponent; let fixture:ComponentFixture; beforeEach(async(()=> {TestBed.configureTestingModule({
声明:[MainComponent,AppTopBarComponent,AppMenuComponent,AppSubMenuComponent],
提供者:[KeycloakService],
导入:[RouterModule,RouterTestingModule,FormsModule,MenuModule]
})compileComponents(); }));
beforeEach(()=> {fixture = TestBed.createComponent(MainComponent); component = fixture.componentInstance; fixture.detectChanges();}); // TODO:期望用户登录但不会发生 - >与KeyCloak有关('应该创建',()=> {expect(component).toBeTruthy();}); });
我没有太多的测试经验,所以我不确定我该怎么做?以某种方式嘲笑钥匙扣?
谢谢!
1 回答
是的,你应该模拟服务:
//在TestBed.configureTestingModule提供程序中:[{provide:KeycloakService,useValu:Mock1}]