How to resolve this Moq error? System.Reflection.TargetParameterCountException: parameter counter mismatch

I use Moq in my nUnit tests.

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 your help.

EDIT: SOLUTION

The following syntax change resolved the issue.

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()); 
+6
source share
1 answer

Change it to

 .Returns(coaDepartments.AsQueryable()); 

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

+11
source

Source: https://habr.com/ru/post/892619/


All Articles