**已关闭。**此问题需要debugging details。它目前不接受回答。
编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答这个问题。
25天前关闭
Improve this question的
我是Jest的新手,遇到了在多个it()块中模拟httpService的问题。在第一个it()块中模拟httpService之后,第二个it()块无法通过。我尝试过使用spyOn,但它与AxiosResponse的效果并不好,即使修复了它,第二个块仍然失败。我还尝试了clearAllMocks()和resetAllMocks(),但它们没有解决这个问题。有人可以帮助我理解和解决这个问题吗?
beforeEach(async () => {
module = await Test.createTestingModule({
providers: [CPDCService, ...providersMock]
}).compile();
service = module.get<CPDCService>(CPDCService);
jest.resetAllMocks();
});
it('should be defined', () => {
expect(service).toBeDefined();
});
describe('submit', () => {
it('should throw BadRequestException when Recaptcha token is invalid', async () => {
const httpService = module.get<HttpService>(HttpService);
const cpdcData: CPDCDto = {
formData: {
recaptchaToken: 'mockRecaptchaToken',
email: 'test@example.com'
},
campaignId: 'mockCampaignId',
bountyId: 'mockBountyId',
formId: 'mockFormId',
referrer: 'google.com',
deviceId: 'mockDeviceId'
};
// Mock the response from the recaptcha verification
const recaptchaResponse = {
data: {
success: false
}
};
// Create a spy on the `post` method of `httpService`
const mockObservable = {
toPromise: () => Promise.resolve(recaptchaResponse)
};
httpService.post = jest.fn().mockImplementation(() => mockObservable);
await expect(service.submit(cpdcData, true, undefined, 'en')).rejects.toThrow(
BadRequestException
);
});
it('should throw NotFoundException when campaign is not found', async () => {
const httpService = module.get<HttpService>(HttpService);
const cpdcData: CPDCDto = {
formData: {
recaptchaToken: 'mockRecaptchaToken',
email: 'test@example.com'
},
campaignId: 'mockCampaignId',
bountyId: 'mockBountyId',
formId: 'mockFormId',
referrer: 'google.com',
deviceId: 'mockDeviceId'
};
// Mock the response from the recaptcha verification
const recaptchaResponse = {
data: {
success: true
}
};
// Create a spy on the `post` method of `httpService`
const mockObservable = {
toPromise: () => Promise.resolve(recaptchaResponse)
};
httpService.post = jest.fn().mockImplementation(() => mockObservable);
await expect(service.submit(cpdcData, true, undefined, 'en')).rejects.toThrow(
NotFoundException
);
});
});
});
字符串
1条答案
按热度按时间pjngdqdw1#
字符串
这起作用了!