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

时间:2011-12-11 03:57:17

标签: c# tdd moq

我正在尝试使用Moq来模拟界面:

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

我在做:

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

但由于错误,它甚至无法编译:

  

错误CS0121:以下方法之间的呼叫不明确或   特性:   'Moq.Language.IReturns&LT; Data.Contract.IMatchSetupRepository,System.Collections.Generic.IEnumerable&LT; Data.Model.MatchSetup&GT;&GT; .Returns(System.Collections.Generic.IEnumerable&LT; Data.Model.MatchSetup&GT)'   和   “Moq.Language.IReturns&LT; Data.Contract.IMatchSetupRepository,System.Collections.Generic.IEnumerable&LT; Data.Model.MatchSetup&GT;&GT; .Returns(System.Func&LT; System.Collections.Generic.IEnumerable&LT; Data.Model.MatchSetup&GT; &GT)'

我正在使用:

  

Moq.dll,v4.0.20926

1 个答案:

答案 0 :(得分:34)

尝试通用版Returns

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

或:

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

相反。因为你传递函数null(并且有两个Returns的重载),编译器不知道你的意思是哪个重载,除非你将参数强制转换为正确的类型。

相关问题