C# 在shim类中调用原始方法

C# 在shim类中调用原始方法,c#,mstest,microsoft-fakes,shim,C#,Mstest,Microsoft Fakes,Shim,我想测试一个存储库,以防出现一些不正常的网络行为。我用MS Fakes伪造了这个类,看起来是这样的: ShimInputRepository .AllInstances .UpdateEngagementStringNullableOfInt64NullableOfInt32String = (xInst, xEngId, xTrimUri, xTargetVers, xComments) =>

我想测试一个存储库,以防出现一些不正常的网络行为。我用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()…”语句),然后将调用转发到原始实例。

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


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

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


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

您要调用哪个方法?我实现的唯一一个方法,即“UpdateEngagement…”是否要从同一方法中最初调用它?这是不可能的,调用自身的代码永远无法到达。或者你想知道如何在该方法之外调用它参见“update”请查看你想调用哪个方法?我实现的唯一一个方法是“UpdateEngagement…”你想从同一个方法中调用它吗?这是不可能的,调用自身的代码永远无法到达。或者你想知道如何在方法之外调用它吗?请看“更新”谢谢,这正是我需要的。谢谢,这正是我需要的。