我有一门我正在尝试测试的课程。该类具有这样的注入服务:
constructor(private actions$: Actions, @Inject('NotificationHandlerService') private notificationService: INotificationHandlerService) {}
在 spec.ts 中,我只是使用一个值来提供它,因为我只想测试显示函数是否被调用:
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
provideMockActions(() => actions$),
RequirementNotificationsEffects,
{
provide: 'NotificationHandlerService',
useValue: {
display: () => {}
}
},
provideMockStore({ initialState: requirementInitialState})
],
});
reqNotificationEffects = TestBed.inject(RequirementNotificationsEffects);
});
然后我正在测试是否调用了显示函数但是我在运行测试时收到错误,说明Cannot spyOn on a primitive value; string given
测试:
it('Call notification service to display message to user.', () => {
const testOwner = new OwnerGroupReference({name: 'Test Owner'}) ;
const action = reqActions.removeOwnersWithSubmission({ owners: [{testOwner}]});
const notificationDisplaySpy = jest.spyOn('NotificationHandlerService', 'display');
actions$ = hot("-a", { a: action });
expect(reqNotificationEffects.removeOwners$).toSatisfyOnFlush(() => {
expect(notificationDisplaySpy).toHaveBeenCalled();
});
});
有人知道我应该做什么吗?