本文介绍了未调用RoleService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试为具有可观察性的组件编写角度代码,但无法测试广播服务内部的角色服务调用.我收到一条错误消息,说未调用该服务.我应该如何使用该服务?任何帮助,将不胜感激.谢谢.
Hi I am trying to write angular code for a component with an observable but I can't test the role service call inside the broadcast service. I get an error saying the service is not being called. How Should I access the service? Any help would be appreciated. Thank you.
这是我的组件,具有可观察性:
ngOnInit(): void {
this.subscription.add(
this.broadcastService.subscribe('msal:acquireTokenSuccess', (payload) => {
// do something here
this.roleService.checkServerEventReviewers().subscribe(res => {
this.userService.userDetails.role = res ? 'Data Steward' : 'Mosaic Consumer';
if (this.isLoggedIn !== true) {
const redirectUri = sessionStorage.getItem('redirectUri');
if (redirectUri !== undefined || redirectUri !== null) {
this.router.navigateByUrl(redirectUri);
}
}
this.isLoggedIn = true;
};
这是我正在尝试的规格文件:
describe('AppComponent', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule],
declarations: [AppComponent],
providers: [WindowService, HttpClient, RoleService, UserService, HttpHandler, BroadcastService, MsalService,
{
provide: MSAL_CONFIG, // MsalService needs config, this provides it.
useFactory: () => ({ // Note this is an arrow fn that returns the config object
redirectUri: window.location.origin + '/',
clientID: mockData.clientID,
}),
}],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
});
it("Role should be Data Steward", fakeAsync (() => {
const fn = 'msal:acquireTokenSuccess';
const subscription = new Subscription();
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
const spyOnBS = spyOn(app.broadcastService,'subscribe');
const roleServiceCall =
spyOn(app.roleService,'checkServerEventReviewers');
app.ngOnInit();
tick();
fixture.whenStable().then(() => {
expect(spyOnBS).toHaveBeenCalled();
expect(roleServiceCall).toHaveBeenCalled();
});
}));
推荐答案
尝试执行以下操作:
describe('AppComponent', () => {
let mockBS: BehaviorSubject<MediaChange>;
beforeEach(() => {
mockBS = new BehaviorSubject();
TestBed.configureTestingModule({
imports: [RouterTestingModule],
declarations: [AppComponent],
providers: [WindowService, HttpClient, RoleService, UserService, HttpHandler, BroadcastService, MsalService,
{ provide: useValue: mockBS.asObservable() },
{
provide: MSAL_CONFIG, // MsalService needs config, this provides it.
useFactory: () => ({ // Note this is an arrow fn that returns the config object
redirectUri: window.location.origin + '/',
clientID: mockData.clientID,
}),
}],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents();
});
it("Role should be Data Steward", fakeAsync (() => {
const fn = 'msal:acquireTokenSuccess';
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
const roleServiceCall = spyOn(app.roleService,'checkServerEventReviewers');
app.ngOnInit();
tick();
fixture.detectChanges();
fixture.whenStable().then(() => {
mockBS.next({});
tick();
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(roleServiceCall).toHaveBeenCalled();
});
});
}));
这篇关于未调用RoleService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!