Farhad-Taran
Farhad-Taran

Reputation: 6512

Moq - Parameter mismatch

I am trying to figure out why I am getting the following exception when Im mocking my very simple interface.

System.Reflection.TargetParameterCountException: Parameter count mismatch.

    var zoneLocator = new Mock<IZoneLocator<ZoneInfo>>();
    zoneLocator
        .Setup(zl => zl.GetZoneInfo(
            It.IsAny<double>(), It.IsAny<double>()))
        .Returns((ZoneInfo zoneInfo) =>
            Task.FromResult(zoneInfo));

    var z = zoneLocator.Object.GetZoneInfo(1, 1);

interface:

public interface IZoneLocator<T>
{
    Task<T> GetZoneInfo(double latitude, double longitude);
}

Upvotes: 2

Views: 249

Answers (1)

Patrick Quirk
Patrick Quirk

Reputation: 23757

The overload of Returns that expects a Func is expecting a function that has the same inputs as the inputs of your mocked method. This allows you to alter the return value based on the inputs to the method.

So, to fix this, change your setup to this:

zoneLocator
    .Setup(zl => zl.GetZoneInfo(It.IsAny<double>(), It.IsAny<double>()))
    .Returns((double latitude, double longitude) =>
        Task.FromResult(/* TODO: create a timezone somehow*/));

Upvotes: 5

Related Questions