[英]Keycloak role based redirection causes unit test error in angular app
问题是您正在为您的单元测试提供真正的KeycloakService
并且这很难做到,因为keycloakService
正在读取 hasResourceRole 的属性/方法,并且此属性/方法所在的hasResourceRole
是未定义的。
我会像这样模拟KeycloakService
:
let fixture: ComponentFixture<MyComponent>;
let mockKeyCloakService: jasmine.SpyObj<KeycloakService>:
beforeEach(async () => {
// The first string argument is optional and it is there for debugging purpose
// where there are errors. The second array of strings are public methods you
// would like to mock.
mockKeyCloakService = jasmine.SpyObj<KeycloakService>('KeycloakService', ['isUserInRole']);
....
providers: [
ConfirmationService,
// provide the mock for the real KeycloakService
{ provide: KeycloakService, useValue: mockKeycloakService },
],
执行上述操作应该有望使其发挥作用。
在此处阅读有关组件测试中的 mocking 服务的信息: https://testing-angular.com/testing-components-depending-on-services/#testing-components-depending-on-services 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.