Search code examples
c#unit-testingasp.net-corexunit

How to mock method that has return type from of the (bool, string)?


The following function has a return value of (bool, string). How to Moq it to be ready for unit testing.

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

Thank you


Solution

  • Just create a value tuple with desired values:

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

    Read more: