Jest.js 为什么httpService的mock在第二个块中没有改变它们的行为,即使使用不同的常量进行mock?[已关闭]

lvjbypge  于 2023-08-01  发布在  Jest
关注(0)|答案(1)|浏览(148)

**已关闭。**此问题需要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
      );
    });
  });
});

字符串

pjngdqdw

pjngdqdw1#

describe('submit', () => {
    it('should throw BadRequestException when Recaptcha token is invalid', async () => {
      const recaptchaResponse = {
        data: {
          success: false
        }
      };

      // Create a spy on the `post` method of `httpService` and mock the implementation
      const promiseMock = jest.fn().mockReturnValueOnce(Promise.resolve(recaptchaResponse));
      jest.spyOn(httpService, 'post').mockReturnValue({ toPromise: promiseMock } as any);

      await expect(service.submit(cpdcData, true, undefined, 'en')).rejects.toThrow(
        BadRequestException
      );
    });
    it('should throw NotFoundException when campaign is not found', async () => {
      const recaptchaResponse = {
        data: {
          success: true
        }
      };

      // Create a spy on the `post` method of `httpService` and mock the implementation
      const promiseMock = jest.fn().mockReturnValueOnce(Promise.resolve(recaptchaResponse));
      jest.spyOn(httpService, 'post').mockReturnValue({ toPromise: promiseMock } as any);

      await expect(service.submit(cpdcData, true, undefined, 'en')).rejects.toThrow(
        NotFoundException
      );
    });
  });

字符串
这起作用了!

相关问题