在shim类中调用原始方法

use*_*063 1 c# mstest shim microsoft-fakes

我想针对一些错误的网络行为测试存储库.我使用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)"
                                        )
                                    );

                        }
                    };
Run Code Online (Sandbox Code Playgroud)

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

如何调用原始方法?

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

Lia*_*amK 5

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

                    }
                };
Run Code Online (Sandbox Code Playgroud)

ShimsContext.ExecuteWithoutShims方法将在当前填充程序上下文之外执行Func <T>(例如,没有导致您无限循环的填充程序重定向).

无限循环的原因是创建ShimContext会在运行时修改程序集.只要上下文处于活动状态,shimmed方法的所有调用都将重定向到shim类上的静态方法.您需要明确地在shim上下文之外寻找您想要正常执行的代码部分.