gpt4 book ai didi

parameters - 我该如何解决这个Moq错误? System.Reflection.TargetParameterCountException : Parameter count mismatch

转载 作者:行者123 更新时间:2023-12-03 09:48:54 24 4
gpt4 key购买 nike

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

最佳答案

更改为

.Returns(coaDepartments.AsQueryable());

(从错误消息中根本看不出来。)

关于parameters - 我该如何解决这个Moq错误? System.Reflection.TargetParameterCountException : Parameter count mismatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6671703/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com