在具有字符串值的提供者上使用 SpyOn
Use SpyOn on provider that has string value
我有一个 class 正在尝试测试。 class 有一个这样注入的服务:
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();
});
});
有人知道我应该做什么吗?
应该是
const notificationDisplaySpy = jest.spyOn(TestBed.get('NotificationHandlerService'), 'display');
在您的情况下,您正在尝试更改字符串的显示处理程序
提取常量变量中的 useValue
const mockService = {
display: jest.fn();
}
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
provideMockActions(() => actions$),
RequirementNotificationsEffects,
{
provide: 'NotificationHandlerService',
useValue: mockService
},
provideMockStore({ initialState: requirementInitialState})
],
});
reqNotificationEffects = TestBed.inject(RequirementNotificationsEffects);
});
it('Call notification service to display message to user.', () => {
const testOwner = new OwnerGroupReference({name: 'Test Owner'}) ;
const action = reqActions.removeOwnersWithSubmission({ owners: [{testOwner}]});
actions$ = hot("-a", { a: action });
expect(reqNotificationEffects.removeOwners$).toSatisfyOnFlush(() => {
expect(mockService.display).toHaveBeenCalled();
});
});
您也可以使用 spyOn 实现此功能
我有一个 class 正在尝试测试。 class 有一个这样注入的服务:
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();
});
});
有人知道我应该做什么吗?
应该是
const notificationDisplaySpy = jest.spyOn(TestBed.get('NotificationHandlerService'), 'display');
在您的情况下,您正在尝试更改字符串的显示处理程序
提取常量变量中的 useValue
const mockService = {
display: jest.fn();
}
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
provideMockActions(() => actions$),
RequirementNotificationsEffects,
{
provide: 'NotificationHandlerService',
useValue: mockService
},
provideMockStore({ initialState: requirementInitialState})
],
});
reqNotificationEffects = TestBed.inject(RequirementNotificationsEffects);
});
it('Call notification service to display message to user.', () => {
const testOwner = new OwnerGroupReference({name: 'Test Owner'}) ;
const action = reqActions.removeOwnersWithSubmission({ owners: [{testOwner}]});
actions$ = hot("-a", { a: action });
expect(reqNotificationEffects.removeOwners$).toSatisfyOnFlush(() => {
expect(mockService.display).toHaveBeenCalled();
});
});
您也可以使用 spyOn 实现此功能