Search code examples
c#.netasync-awaitmoq

Mocking ReturnsAsync using Moq to return 2 values


Given a interface:

Task<Booking> GetBookingAsync(Guid reservationId);

I would mock the following like so:

_bookingLogic.Setup(x => x.GetBookingAsync(It.IsAny<Guid>())).ReturnsAsync(new Booking());

Givent the interface now changes to:

Task<(Booking Booking, IList<GAEvent> GaEvents)> GetBookingAsync(Guid reservationId);

How would this be mocked using Moq?

_bookingLogic.Setup(x => x.GetBookingAsync(It.IsAny<Guid>())).ReturnsAsync(?????);

Solution

  • In second case result is a value tuple so you need to create one. Try:

    .ReturnsAsync((new Booking(), (IList<GAEvent>)new List<GAEvent>()))