user1082693
user1082693

Reputation: 621

Setup method in Moq, ambiguous call

I'm trying to use Moq to mock the interface:

public interface IMatchSetupRepository
{
    IEnumerable<MatchSetup> GetAll();
}

and I'm doing:

var matchSetupRepository = new Mock<IMatchSetupRepository>();
matchSetupRepository
    .Setup(ms => ms.GetAll())
    .Returns(null);

But it doesn't even compile because of the error:

error CS0121: The call is ambiguous between the following methods or properties: 'Moq.Language.IReturns<Data.Contract.IMatchSetupRepository,System.Collections.Generic.IEnumerable<Data.Model.MatchSetup>>.Returns(System.Collections.Generic.IEnumerable<Data.Model.MatchSetup>)' and 'Moq.Language.IReturns<Data.Contract.IMatchSetupRepository,System.Collections.Generic.IEnumerable<Data.Model.MatchSetup>>.Returns(System.Func<System.Collections.Generic.IEnumerable<Data.Model.MatchSetup>>)'

I'm using:

Moq.dll, v4.0.20926

Upvotes: 36

Views: 11901

Answers (1)

Andrew Whitaker
Andrew Whitaker

Reputation: 126042

Try the generic version of Returns:

var matchSetupRepository = new Mock<IMatchSetupRepository>();
matchSetupRepository
    .Setup(ms => ms.GetAll())
    .Returns<IEnumerable<MatchSetup>>(null);

or:

var matchSetupRepository = new Mock<IMatchSetupRepository>();
matchSetupRepository
    .Setup(ms => ms.GetAll())
    .Returns((IEnumerable<MatchSetup>)null);

Instead. Because you're passing the function null (and there are two overloads of Returns), the compiler does not know which overload you mean unless you cast the argument to the correct type.

Upvotes: 57

Related Questions