Moq 无法实例化包含 lambda 表达式的服务
Moq cannot instantiate service that includes lambda expression
我想用 Moq 模拟以下方法:
T GetOne(Expression<Func<T, bool>> expression);
在以下方法中调用:
public GetTCNotifyFCResponse GetTCNotifyFC(string operationNumber)
{
var response = new GetTCNotifyFCResponse { IsValid = false };
try
{
var tcAbstract = _tcAbstractRepository
.GetOne(x => x.Operation.OperationNumber == operationNumber);
if (tcAbstract == null)
{
response.ErrorMessage = Localization.GetText(WORKFLOW_DONT_STARED);
return response;
}
[...]
测试代码为:
var mockAbstractRep = new Mock<ITCAbstractRepository>();
mockAbstractRep
.Setup(s => s.GetOne(x => x.Operation.OperationNumber == operationNumber))
.Returns(entity);
但是当 运行 时,我得到一个空 "tcAbstract" 结果... "operationNumber" 和 "entity" 变量之前已填充,为简单起见未包含在此处。
我做错了什么?
试试看是否有帮助
var mockAbstractRep = new Mock<ITCAbstractRepository>();
mockAbstractRep
.Setup(s => s.GetOne(It.IsAny<Expression<Func<EntityType, bool>>>()))
.Returns(entity);
将 EntityType
替换为您的方法需要的类型
我想用 Moq 模拟以下方法:
T GetOne(Expression<Func<T, bool>> expression);
在以下方法中调用:
public GetTCNotifyFCResponse GetTCNotifyFC(string operationNumber)
{
var response = new GetTCNotifyFCResponse { IsValid = false };
try
{
var tcAbstract = _tcAbstractRepository
.GetOne(x => x.Operation.OperationNumber == operationNumber);
if (tcAbstract == null)
{
response.ErrorMessage = Localization.GetText(WORKFLOW_DONT_STARED);
return response;
}
[...]
测试代码为:
var mockAbstractRep = new Mock<ITCAbstractRepository>();
mockAbstractRep
.Setup(s => s.GetOne(x => x.Operation.OperationNumber == operationNumber))
.Returns(entity);
但是当 运行 时,我得到一个空 "tcAbstract" 结果... "operationNumber" 和 "entity" 变量之前已填充,为简单起见未包含在此处。
我做错了什么?
试试看是否有帮助
var mockAbstractRep = new Mock<ITCAbstractRepository>();
mockAbstractRep
.Setup(s => s.GetOne(It.IsAny<Expression<Func<EntityType, bool>>>()))
.Returns(entity);
将 EntityType
替换为您的方法需要的类型