如何解决此Moq错误? System.Reflection.TargetParameterCountException:参数计数不匹配

时间:2011-07-12 21:57:45

标签: parameters moq

我在我的nUnit测试用例中使用Moq。

以下是我的测试用例:

        IList<ChartFieldDepartment> coaDepartments = new List<ChartFieldDepartment>() {
                new ChartFieldDepartment { ChartFieldKey="1000", Description="Corporate Allocation"},
                new ChartFieldDepartment { ChartFieldKey="1010", Description="Contribution to Capital"}
        };

        Mock<IChartFieldRepository> mockChartFieldRepository = new Mock<IChartFieldRepository>();
        mockChartFieldRepository.Setup(x => x.RetrieveChartFieldDepartments(It.IsAny<bool>())).Returns(coaDepartments.AsQueryable);

        ChartFieldDomainService chartFieldDomainService = new ChartFieldDomainService(mockChartFieldRepository.Object);

        // this line fails! I get System.Reflection.TargetParameterCountException : Parameter count mismatch
        IQueryable<ChartFieldDepartment> departments = chartFieldDomainService.RetrieveChartFieldDepartments();

这是我的ChartFieldDomainService

public class ChartFieldDomainService : IChartFieldDomainService
{
    private IChartFieldRepository _chartFieldRepository = null;

    public ChartFieldDomainService(IChartFieldRepository repository)
    {
        _chartFieldRepository = repository;
    }

    public virtual IQueryable<ChartFieldDepartment> RetrieveChartFieldDepartments()
    {
        return _chartFieldRepository.RetrieveChartFieldDepartments(true); // always refresh, get latest
    }
    //....
 }

提前感谢您的帮助。

编辑:解决方案

以下语法更改解决了问题。

原帖:

        mockChartFieldRepository.Setup(x => x.RetrieveChartFieldDepartments(It.IsAny<bool>()))
            .Returns(coaDepartments.AsQueryable());

更新后的行:

        mockChartFieldRepository.Setup(x => x.RetrieveChartFieldDepartments(It.IsAny<bool>()))
            .Returns((bool x) => coaDepartments.AsQueryable());

1 个答案:

答案 0 :(得分:11)

将其更改为

.Returns(coaDepartments.AsQueryable());

(从错误消息中可以看出这一点。)