Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any alternative for Microsoft Fakes in .NET Core?

Tags:

I am looking for an alternative to Microsoft Fakes in .NET Core. I know it is no longer supported in .NET Core. I just do not understand why not, I think it was a good solution in certain situations.

My problem is that I want to mock DateTime.Now. Previously you could do this with the following code:

System.Fakes.ShimDateTime.NowGet = () => 
{ 
   return new DateTime(2000, 1, 1); 
};

It is described in the Microsoft documentation, see the link for more information: https://docs.microsoft.com/en-us/visualstudio/test/using-shims-to-isolate-your-application-from-other-assemblies-for-unit-testing?view=vs-2017

For now I solved it by creating a wrapper for DateTime, which looks like this:

/// <summary>
/// Used for getting DateTime.Now(), time is changeable for unit testing
/// </summary>
public static class SystemTime
{
   /// <summary> 
   /// Normally this is a pass-through to DateTime.Now, but it can be 
   /// overridden with SetDateTime( .. ) for testing or debugging.
   /// </summary>
   public static Func<DateTime> Now = () => DateTime.Now;

   /// <summary> 
   /// Set time to return when SystemTime.Now() is called.
   /// </summary>
   public static void SetDateTime(DateTime dateTimeNow)
   {
      Now = () =>  dateTimeNow;
   }

   /// <summary> 
   /// Resets SystemTime.Now() to return DateTime.Now.
   /// </summary>
   public static void ResetDateTime()
   {
       Now = () => DateTime.Now;
   }
}

I owe this solution to the next StackOverflow post: Unit Testing: DateTime.Now

But I am not satisfied with this solution yet, because I feel I have to adjust my implementation for my testing. I do not think this is desirable.

I hope someone can help me with this, thanks in advance for the effort.

like image 240
Undeadparade Avatar asked Sep 25 '18 11:09

Undeadparade


People also ask

What are fakes in C#?

The Fakes framework uses delegate-based methods for writing code. There are the following two types in the Fakes Framework: Stub: the Stub type is used for many kinds of classes and interfaces that have overridable methods. Shim: the Shim type is used for many kinds of static, sealed, and non-overridable methods.

What is a shim C#?

What Does Shim Mean? Shim, in C#, is a template class that is derived from a base class with derived classes that inherit the data and behavior of the base class and vary only in the type. The derived class of the shim class reuses the implementation provided by the shim class.

Is .NET core from Microsoft?

NET Framework is at version 4.8 and remains fully supported by Microsoft. In 2014, Microsoft introduced . NET Core as a cross-platform, open-source successor to . NET Framework.

What is Windows Netcore?

NET Core is a new version of . NET Framework, which is a free, open-source, general-purpose development platform maintained by Microsoft. It is a cross-platform framework that runs on Windows, macOS, and Linux operating systems.


2 Answers

Pose works well for this.

using Pose;

Shim dateTimeShim = Shim.Replace(() => DateTime.Now).With(() => new DateTime(2004, 4, 4));

// This block executes immediately
PoseContext.Isolate(() =>
{
    // All code that executes within this block
    // is isolated and shimmed methods are replaced

    // Outputs "4/4/04 12:00:00 AM"
    Console.WriteLine(DateTime.Now);

}, dateTimeShim);
like image 162
Chris Gessler Avatar answered Sep 22 '22 18:09

Chris Gessler


I am looking for an alternative to Microsoft Fakes in .NET Core. I know it is no longer supported in .NET Core. I just do not understand why not, I think it was a good solution in certain situations.

Since May 19th 2020 Microsoft Fakes supports .NET Core.

https://docs.microsoft.com/en-us/visualstudio/releases/2019/release-notes#16.6.0

like image 28
Francesco B. Avatar answered Sep 20 '22 18:09

Francesco B.