Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

parameters

moq

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)
    {
        _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());
like image 825
Raymond Avatar asked Jul 12 '11 21:07

Raymond


1 Answers

Change it to

.Returns(coaDepartments.AsQueryable());

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

like image 132
TrueWill Avatar answered Oct 23 '22 01:10

TrueWill