【问题标题】: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;
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      • 2015-12-22
      • 2010-11-18
      • 2013-12-23
      • 2014-12-06
      • 2016-06-16
      • 1970-01-01
      相关资源
      最近更新 更多