Calling the original method from the pad method

When creating pads for type members in BCL (or any other library). We often come across a situation where we want to call the original method that we redefined (whether inside the pad delegate or outside). For example:.

System.Fakes.ShimDateTime.NowGet = () => DateTime.Now.AddDays(-1);

In the above code, all we want to do when DateTime.Now is called is to return a day less than the actual date. Perhaps this seems like a far-fetched example, so other (more) realistic scenarios

  • The ability to capture and verify the values โ€‹โ€‹of arguments passed to a particular method.
  • To count the number of times a particular method / property accesses the code under test.

I ran into the last scenario in a real application and could not find the answer for Fakes on SO. However, after you unearthed the Fakes documentation, I found the answer by posting it along with a community question.

+4
source share
1 answer

For fakes there is built-in support; There are actually two ways to achieve this.

1) Use ShimsContext.ExecuteWithoutShims () as a wrapper for code in which you don't need padding behavior:

System.Fakes.ShimDateTime.NowGet = () => 
{
return ShimsContext.ExecuteWithoutShims(() => DateTime.Now.AddDays(-1));
};

2) Another approach is to set the gasket to zero, call the original method, and restore the gasket.

FakesDelegates.Func<DateTime> shim = null;
shim = () => 
{
System.Fakes.ShimDateTime.NowGet = null;
var value = ShimsContext.ExecuteWithoutShims(() => DateTime.Now.AddDays(-1));
System.Fakes.ShimDateTime.NowGet = shim;
return value;
};
System.Fakes.ShimDateTime.NowGet = shim;

: , . / .

+8

All Articles