【问题标题】:Twilio API mocking unit testing in Nestjs using Jest使用 Jest 在 Nestjs 中进行 Twilio API 模拟单元测试
【发布时间】:2021-06-09 14:51:17
【问题描述】:

我有这个twilio.service.ts 文件。我想在twilio.service.spec.ts 中编写一个单元测试,用于验证 OTP 已成功发送或因缺少参数而失败的两种情况。

twilio.services.ts

  async startVerification(
   startVerificationDto: StartVerificationDto
   ) {
    try {
      const verificationResponse = await this.twilioClient.verify
        .services("TWILIO_VERIFY_SERVICE_SID")
        .verifications.create({
          to: `+91${sendVerificationCodeDto.phoneNumber}`,
          channel: "sms",
          locale: "en",
        });
      return verificationResponse;
    } catch (error) {
      throw new BadRequestException("Failed sending OTP");
    }
  }

到目前为止尝试过:

it("Should be able to send verification code on mobile number", async () => {
    const actualRes = await controller.startVerification({
      phoneNumber: "xxxxxxxxxxx",
    });

    expect(twilioService.twilioClient.verify.services).toBeCalledWith(
      "TWILIO_VERIFY_SERVICE_SID"
    );

    expect(actualRes).toEqual({
      sid: "VA_TWILIO_VERIFY_SERVICE_SID",
    });
  });



  it("Should return error if required parameters are missing", async () => {

    expect(twilioService.twilioClient.verify.services).not.toBeCalledWith(
      "TWILIO_VERIFY_SERVICE_SID"
    );

    try {
      await controller.startVerification({
        phoneNumber: "xxxxxxxxxxx",
      });
    } catch (e) {
      expect(e.message).toBe("Failed sending OTP");
    }
  });

我主要关心的是:如何模拟 Twilio 的嵌套函数(验证>服务>验证>创建)。 另外,在嘲笑我如何在 spec.ts 文件中调用或使用它之后。

【问题讨论】:

    标签: typescript unit-testing jestjs twilio twilio-api


    【解决方案1】:

    这里是 Twilio 开发者宣传员。

    模拟嵌套对象和函数调用比困难更烦人。我认为这样的事情应该适合你:

    (在您的示例中,我不确定controllertwilioService 之间的关系。我尝试在此处模拟twilioService 上的twilioClient,但您可能需要根据关于事物如何组合在一起。)

    it("Should be able to send verification code on mobile number", async () => {
      const mockVerificationCreate = jest.fn().mockResolvedValue({
        sid: "VA_TWILIO_VERIFY_SERVICE_SID"
      })
      const mockVerifyService = jest.fn().mockReturnValue({
        verifications: {
          create: mockVerificationCreate
        }
      })
      twilioService.twilioClient = {
        verify: {
          services: mockVerifyService
        }
      }
     
      const actualRes = await controller.startVerification({
        phoneNumber: "xxxxxxxxxxx",
      });
    
      expect(twilioService.twilioClient.verify.services).toBeHaveBeenCalledWith(
        "TWILIO_VERIFY_SERVICE_SID"
      );
      expect(mockVerificationCreate).toHaveBeenCalledWith({
        to: "+91xxxxxxxxxxx",
        channel: "sms",
        locale: "en",
      });
      expect(actualRes).toEqual({
        sid: "VA_TWILIO_VERIFY_SERVICE_SID",
      });
    });
    

    如果有帮助,请告诉我。

    【讨论】:

    • 感谢@philnash 回复。这里servicestwilioService.twilioClient = { verify: { services: mockVerifyService } } 中抛出了一个打字稿错误:Type 'Mock<any, any>' is missing the following properties from type 'ServiceListInstance': create, each, get, getPage, and 3 more.ts(2740) Verify.d.ts(25, 12): The expected type comes from property 'services' which is declared here on type 'Verify' 。如何解决这个问题?
    猜你喜欢
    • 2020-05-15
    • 1970-01-01
    • 2018-09-30
    • 2021-11-05
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 2019-09-01
    相关资源
    最近更新 更多