Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup method in Moq, ambiguous call

Tags:

c#

tdd

moq

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

like image 242
user1082693 Avatar asked Dec 11 '11 03:12

user1082693


1 Answers

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.

like image 61
Andrew Whitaker Avatar answered Oct 08 '22 13:10

Andrew Whitaker