问题
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.
This is my component with the observable:
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;
};
This is my spec file that I am trying:
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();
});
}));
回答1:
If you are facing error in your component file not spec file, then store this into local variable and use it with your roleService
Updated code will looks like
ngOnInit(): void {
const that = this; // STORE this into local variable
this.subscription.add(
this.broadcastService.subscribe('msal:acquireTokenSuccess', (payload) => {
...
// USE "that" instead of "this"
that.roleService.checkServerEventReviewers().subscribe(res => {
...
});
});
);
回答2:
Try doing this:
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();
});
});
}));
来源:https://stackoverflow.com/questions/58864138/roleservice-not-being-called