我想用 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);
但是在运行它时我会得到一个null&#34; tcAbstract&#34;结果......&#34; operationNumber&#34;和&#34;实体&#34;之前填充变量,为简单起见,这里没有包含变量。
我做错了什么?
答案 0 :(得分:1)
试试这个,看看是否有帮助
var mockAbstractRep = new Mock<ITCAbstractRepository>();
mockAbstractRep
.Setup(s => s.GetOne(It.IsAny<Expression<Func<EntityType, bool>>>()))
.Returns(entity);
将EntityType
替换为您的方法所需的类型