【发布时间】:2018-02-12 10:10:42
【问题描述】:
您好,我正在尝试模拟以下内容:
var result = _scope.Execute<FooService, IList<FooEntity>>(x => x.GetFooEntities(fooModel));
这就是我尝试模拟它的方式:
_mockedScope
.Setup(x => x.Execute<FooService, IList<FooEntity>>(f => f.GetFooEntities(It.IsAny<FooModel>())))
.Returns(new List<FooEntity>)
但是当我运行测试时它会抛出一个异常
不支持的表达式:s => s.GetFooEntities(IsAny())
有什么建议可以模拟它吗?
这是我想要起订量的示例
public class Test
{
private readonly IScope _scope;
public Test(IScope scope)
{
_scope = scope;
}
public void Foo()
{
var foo = new FooEntity();
Result<IList<Foo>> result =
_scope.Execute<FooService, IList<Foo>>(
"f",
s => s.GetFoo(foo));
}
}
public class Foo
{
}
public class FooEntity
{
}
public class FooService
{
public List<Foo> GetFoo(FooEntity f);
}
public interface IScope
{
Result<TResult> Execute<T1, TResult>(string temp, Func<T1, TResult> function);
}
public class Result<T>
{
private Result(T value, Exception exception)
{
Value = value;
Error = exception;
}
public Exception Error { get; }
public T Value { get; }
public bool HasError => Error != null;
public static Result<T> Fail(Exception exception) => new Result<T>(default(T), exception);
public static Result<T> Success(T value) => new Result<T>(value, null);
}
【问题讨论】:
-
如果你能提供一个minimal reproducible example 那就太好了,这样我们就可以将完整的示例复制并粘贴到控制台应用程序中。
-
另外,也许您可以解释一下为什么您要按自己的方式模拟事物,而不是模拟
FooService? -
您的示例中没有
s。你能告诉我xf和s是什么吗? -
@mjwills 我已经添加和示例。
标签: c# unit-testing mocking moq xunit