【问题标题】:Shim use default method implementationShim 使用默认方法实现
【发布时间】:2023-04-04 09:19:01
【问题描述】:
我可以从我的 shim 内部以某种方式调用默认方法吗?
我试过了,但它不起作用。
ShimConfigurationHelper.GetConfigValueString = (key) {
switch (key)
{
case "SpecialKey":
return "some-value-for-testing";
default:
return ConfigurationHelper.GetConfigValue(key);
}
};
【问题讨论】:
标签:
c#
unit-testing
nunit
microsoft-fakes
shim
【解决方案1】:
您可以标记要执行的代码部分,就像它们在 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;
};