测试广播服务



嗨,我正试图为一个具有可观察对象的组件编写角度代码,但我无法测试广播服务。我收到一个错误,说没有调用该服务。我应该如何访问该服务?如有任何帮助,我们将不胜感激。非常感谢。

这是我的可观察组件:

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();
});
describe(':', () => {
function setup() {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
const compiled = fixture.debugElement.nativeElement;
return {fixture, app, compiled};
}
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();
});
}));

对不起,我看错了这个问题。(回复已更新(。我认为问题在于你正在测试一个可观察的服务。我认为你应该使用fakeAsync并勾选来测试它。

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();
});
describe(':', () => {
function setup() {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.debugElement.componentInstance;
const compiled = fixture.debugElement.nativeElement;
return {fixture, app, compiled};
}
it('Init with QA environment', fakeAsync(() => {
const {app} = setup();
spyOnBS = spyOn(app.broadcastService,'subscribe');
spyOn(app.authService, 'getUser').and.returnValue(mockData.userDetails);
spyOn(app.authService, 'acquireTokenSilent').and.returnValue('msal:acquireTokenSuccess');
app.ngOnInit();
tick();
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(spyOnBS).toHaveBeenCalled();
});
));

试试看是否有效。

最新更新