Подтвердить что ты не робот

Метод установки в Moq, двусмысленный вызов

Я пытаюсь использовать Moq для издевательства интерфейса:

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

и я делаю:

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

Но он даже не компилируется из-за ошибки:

ошибка CS0121: вызов неоднозначен между следующими способами или свойства: 'Moq.Language.IReturns < Data.Contract.IMatchSetupRepository, System.Collections.Generic.IEnumerable < Data.Model.MatchSetup → .Returns(System.Collections.Generic.IEnumerable < Data.Model.MatchSetup > )' а также "Moq.Language.IReturns < Data.Contract.IMatchSetupRepository, System.Collections.Generic.IEnumerable < Data.Model.MatchSetup → .Returns(System.Func < System.Collections.Generic.IEnumerable < Data.Model.MatchSetup → ) '

Я использую:

Moq.dll, v4.0.20926

4b9b3361

Ответ 1

Попробуйте стандартную версию 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), компилятор не знает, какую перегрузку вы имеете в виду, если вы не передадите аргумент правильному типу.