【问题标题】:Calling original method in shim class在 shim 类中调用原始方法
【发布时间】:2015-05-11 02:26:16
【问题描述】:

我想针对某些错误的网络行为测试存储库。我使用 MS Fakes 伪造了课程,它看起来像这样:

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

                        }
                    };

我不知道如何使用该代码调用原始方法(目前它递归调用相同的方法)。

如何调用原方法?

更新:我真正想要实现的是在对该方法的特定调用(“if() ...”语句)上抛出异常,否则将调用转发到原始实例。

【问题讨论】:

  • 你想调用哪个方法?
  • 我实现的唯一一个,即“UpdateEngagement...”
  • 你想从同一个方法中调用它吗?这是不可能的,永远不会到达调用自身的代码。或者您想知道如何在该方法之外调用它
  • 请查看“更新”

标签: c# mstest microsoft-fakes shim


【解决方案1】:

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 方法将在当前 shim 上下文之外执行 Func(例如,没有导致无限循环的 shim 重定向)。

无限循环的原因是创建 ShimContext 会在运行时修改您的程序集。只要上下文处于活动状态,所有 shimmed 方法的调用都会重定向到 shim 类上的静态方法。对于要正常执行的代码部分,您需要显式地超出 shim 上下文。

【讨论】:

  • 谢谢,这正是我所需要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多