user3284063
user3284063

Reputation: 685

Calling original method in shim class

I want to test a repository against some erronous network behavior. I faked the class using MS Fakes and it looks like this:

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

                        }
                    };

I don't know how to call the original method from withing that code (currently it recursively calls same method).

How to call the original method?

UPDATE: What I really want to achieve is to throw an exception on a specific call to this method (the "if() ..." statement) and forward the call to original instance otherwise.

Upvotes: 1

Views: 1818

Answers (1)

LiamK
LiamK

Reputation: 815

The Fakes framework provides support for just such an occasion. You can use:

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

                    }
                };

The ShimsContext.ExecuteWithoutShims method will execute the Func< T > outside of the current shim context (e.g. without the shim redirection which was causing you your infinite loop).

The cause of your infinite loop is that creating a ShimContext modifies your assembly at runtime. As long as the context is active, all invocations of the shimmed method are redirected to the static method on the shim class. You need to explicitly go outside the shim context for the portion of code you want to execute as normal.

Upvotes: 6

Related Questions