2015-04-17 28 views
1

我想測試一個存儲庫,以防止某些錯誤的網絡行爲。我使用僞造的假貨MS的類,它看起來像這樣:在填充類中調用原始方法

ShimInputRepository 
       .AllInstances 
       .UpdateEngagementStringNullableOfInt64NullableOfInt32String = (xInst, xEngId, xTrimUri, xTargetVers, xComments) => 
        { 


         if (xEngId != initializer.SeededEngagementsWithoutEmp[3].EngagementId) 
         { 
          return xInst.UpdateEngagement(xEngId, xTrimUri, xTargetVers, xComments); //Unfortunately, calls recursively same method (not the original one) 
         } 
         else 
         { 
          throw new Exception 
            (
             "An error occurred while executing the command definition. See the inner exception for details.", 
             new Exception 
             (
              "A transport-level error has occurred when receiving results from the server. (provider: Session Provider, error: 19 - Physical connection is not usable)" 
             ) 
            ); 

         } 
        }; 

我不知道如何從withing代碼調用原來的方法(目前遞歸調用同樣的方法)。

如何調用原始方法?

更新:我真正想實現的是在對此方法的特定調用(「if()...」語句)上引發異常,否則將調用轉發給原始實例。

+0

你想調用哪種方法? – mbdavis

+0

我實現的唯一一個,那就是「UpdateEngagement ...」 – user3284063

+0

你想在同一個方法中調用它嗎?這是不可能的,自己調用的代碼永遠不會被達到。或者你想知道如何在該方法之外調用它 – mbdavis

回答

5

Fakes框架爲這種場合提供支持。您可以使用:

ShimInputRepository 
      .AllInstances 
      .UpdateEngagementStringNullableOfInt64NullableOfInt32String = (xInst, xEngId, xTrimUri, xTargetVers, xComments) => 
       { 


        if (xEngId != initializer.SeededEngagementsWithoutEmp[3].EngagementId) 
        { 
         return ShimsContext.ExecuteWithoutShims(() => xInst.UpdateEngagement(xEngId, xTrimUri, xTargetVers, xComments)); 
        } 
        else 
        { 
         throw new Exception 
           (
            "An error occurred while executing the command definition. See the inner exception for details.", 
            new Exception 
            (
             "A transport-level error has occurred when receiving results from the server. (provider: Session Provider, error: 19 - Physical connection is not usable)" 
            ) 
           ); 

        } 
       }; 

的ShimsContext.ExecuteWithoutShims方法將執行Func鍵< T>當前墊片上下文之外(例如不墊片重定向這是使你的無限循環)。

無限循環的原因在於創建ShimContext會在運行時修改您的程序集。只要上下文處於活動狀態,shimmed方法的所有調用都將重定向到shim類上的靜態方法。您需要顯式地超出正常情況下要執行的部分代碼的填充上下文。

+0

謝謝,這正是我所需要的。 – user3284063

相關問題