简体   繁体   English

Moq中的设置方法,模糊调用

[英]Setup method in Moq, ambiguous call

I'm trying to use Moq to mock the interface: 我正在尝试使用Moq来模拟界面:

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>>)' 错误CS0121:以下方法或属性之间的调用不明确:'Moq.Language.IReturns <Data.Contract.IMatchSetupRepository,System.Collections.Generic.IEnumerable <Data.Model.MatchSetup >>。返回(System.Collections.Generic .IEnumerable <Data.Model.MatchSetup>)'和'Moq.Language.IReturns <Data.Contract.IMatchSetupRepository,System.Collections.Generic.IEnumerable <Data.Model.MatchSetup >>。返回(System.Func <System.Collections .Generic.IEnumerable <Data.Model.MatchSetup >>)”

I'm using: 我正在使用:

Moq.dll, v4.0.20926 Moq.dll,v4.0.20926

Try the generic version of Returns : 尝试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. 因为您传递函数null(并且有两个Returns重载),编译器不知道您的意思是哪个重载,除非您将参数强制转换为正确的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM