开发者

How do I resolve this Moq error? System.Reflection.TargetParameterCountException : Parameter count mismatch

I am using Moq in my nUnit test cases.

Here's what my test case looks like:

        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();

Here is my ChartFieldDomainService:

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

    public ChartFieldDomainService(IChartFieldRepository repository)开发者_如何学Go
    {
        _chartFieldRepository = repository;
    }

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

Thanks in advance for the help.

EDIT: SOLUTION

The following change in syntax fixed the problem.

Original Line:

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

Updated Line:

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


Change it to

.Returns(coaDepartments.AsQueryable());

(Which is not at all obvious from the error message.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜