2015-01-13 51 views
1

可以以某种方式从我的垫片的内部调用默认方法吗?垫片使用默认方法实现

我试过这个,但它不起作用。

ShimConfigurationHelper.GetConfigValueString = (key) { 
    switch (key) 
    {      
     case "SpecialKey": 
      return "some-value-for-testing"; 

     default: 
      return ConfigurationHelper.GetConfigValue(key); 
    } 
}; 

回答

0

您可以将您的代码的部分标记为执行,就好像它们在ShimContext之外,以便它们将使用原始实现。执行此操作的一种方法是将代码从代码中打包到ShimContext.ExecuteWithoutShims。这样做,你的代码可能看起来像这样::

ShimConfigurationHelper.GetConfigValueString = (key) { 
    var response=String.Empty; 
    switch (key) 
    {      
     case "SpecialKey": 
      response = "some-value-for-testing"; 
      break; 

     default: 
      ShimsContext.ExecuteWithoutShims(() => { 
       response = ConfigurationHelper.GetConfigValue(key); 
      }); 
      break; 
    } 
    return response; 
};