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());
Change it to
.Returns(coaDepartments.AsQueryable());
(Which is not at all obvious from the error message.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With