代码之家  ›  专栏  ›  技术社区  ›  M Javad Salehi

如何模拟返回类型为from(bool,string)的方法?

  •  1
  • M Javad Salehi  · 技术社区  · 2 年前

    以下函数的返回值为 (bool, string) .如何对其进行Moq,以便为单元测试做好准备。

    Task<(bool, string)> CreatePhoneCertificate(
        int orderId, 
        CancellationToken cancellationToken);
    
       var mockInsuranceService = new Mock<IInsuranceService>();
       mockInsuranceService
      .Setup(x => x.CreatePhoneCertificate(12345, It.IsAny<CancellationToken>()))
      .ReturnsAsync(???);
    

    非常感谢。

    2 回复  |  直到 2 年前
        1
  •  3
  •   Guru Stron    2 年前

    只需创建一个具有所需值的值元组:

    mockInsuranceService.Setup(x => x.CreatePhoneCertificate(12345, It.IsAny<CancellationToken>()))
        .ReturnsAsync((true, "someString"));
    

    阅读更多: