我有以下用于DI和IOC Web服务客户端的接口
public interface IWcfServiceClientProvider <TContract>: IDisposable where TContract: class
{
TResult Execute<TResult>(Expression<Func<TContract, TResult>> expression);
TResult Execute<TResult>(Expression<Func<TContract, TResult>> expression, bool closeConnection = true);
void Execute(Expression<Action<TContract>> expression);
void Execute(Expression<Action<TContract>> expression, bool closeConnection = true);
}
在我的测试课程中,我有以下内容:
List<BaseLookup> myList = new List<BaseLookup> {
new BaseLookup { Id =1, Code = "1001"},
new BaseLookup { Id =2, Code = "1002"},
new BaseLookup { Id =3, Code = "1003"}};
在我的测试方法中
Mock<IWcfServiceClientProvider<ILookupService>> lookupServiceClinetProvider = new Mock<IWcfServiceClientProvider<ILookupService>>();
var controller = new ElectorSearchController(lookupServiceClinetProvider.Object);
lookupServiceClinetProvider.Setup(mock => mock.Execute(lookup => lookup.GetList(10))).Returns(myList).Verifiable();
var list = controller.testMethod();
lookupServiceClinetProvider.VerifyAll();
列表仅在GetList
的参数设置为10时才有值,即GetList(10)
非GetList(i)
,其中i = 10
以下作品
lookupServiceClinetProvider.Setup(mock => mock.Execute(It.IsAny<Expression<Func<ILookupService, List<BaseLookup>>>>(), true )).Returns((List<BaseLookup>)myList).Verifiable();
但是我想模仿GetList
的调用,而不是任何对Execute的调用。如果可行,那么我可以过滤Return
方法
答案 0 :(得分:0)
作为一个临时解决方案,我写了一个简单的暴力解决方案来比较以下表达式:
public static bool ExpressionMatcher<TIn, TResult>(Expression<Func<TIn, TResult>> expr1, Expression<Func<TIn, TResult>> expr2)
{
if (expr1 == null || expr2 == null)
{
return false;
}
if (expr1 == expr2)
{
return true;
}
if (expr1.Type != expr2.Type)
{
return false;
}
if (expr1.Body == expr2.Body)
{
return true;
}
if (expr1.Body.NodeType != expr2.Body.NodeType)
{
return false;
}
if (expr1.Body.NodeType == ExpressionType.Call)
{
dynamic expr1Body = expr1.Body;
dynamic expr2Body = expr2.Body;
if (expr1Body.Method.Name == expr2Body.Method.Name &&
expr1Body.Method.ReturnType == expr2Body.Method.ReturnType)
{
if (expr1Body.Arguments == null && expr2Body.Arguments == null)
{
return true;
}
if (expr1Body.Arguments.Count != expr2Body.Arguments.Count)
{
return false;
}
return true;
}
}
return false;
}
我使用以下内容来称呼它
Expression<Func<ILookupService, List<BaseLookup>>> expr = lookup => lookup.GetMyList(It.IsAny<long>());
.Setup(mock => mock.Execute(It.Is<Expression<Func<ILookupService, List<BaseLookup>>>>(method => ExpressionMatcher(method, expr))))
.Returns(myList)
.Verifiable();
我不需要查看论据&#39;在这一点上打字。如果您有更好的答案,请告诉我