2013-04-18 53 views
0

起訂量函數調用,我有以下接口,用於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而不是任何調用執行。如果這樣的作品,那麼我可以在Return方法

+0

有人知道我失蹤了嗎?謝謝 –

+0

我也試過嘲笑ILookupService。但是執行不匹配。我在http://stackoverflow.com/questions/16124263/moq-linq-expression-in-repository-specify-expression-in-setup嘗試解決方案仍然無法正常工作。 –

回答

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

我不需要在這一點檢查參數的類型。如果你有更好的答案,請讓我知道