Reputation: 621
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
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