Any alternative for Microsoft Fakes in .NET Core?

Related searches

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.

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

Any alternative for Microsoft Fakes in .NET Core?, 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,� For developers using Microsoft Visual Studio, the most basic choice is whether to use VS' built-in Microsoft Fakes framework, or a third-party/open source framework such as Moq or NSubstitute. In this post, we'll take a look at what makes MS Fakes stand out from other mock frameworks, and when Fakes may or may not be your best choice for testing.

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

Support Microsoft Fakes on .NET Core, Support Microsoft Fakes on .NET Core. Closed - Fixed fixed in: visual studio 2019 version 16.6 preview 3fixed Please provide support to use Microsoft Fakes Stub/Shim context in . @Kendra Havens [MSFT] For now do we have any alternative. Fixed with no extra steps. 0. No, I tried everything. Please re-open this. 5. 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.

Thanks for all the comments, that certainly helped me. I have slightly modified my implementation;

The SystemTime class is now called DateTimeProvider and looks like this:

/// <summary>
/// Used for getting DateTime.Now(), time is changeable for unit testing
/// </summary>
public class DateTimeProvider : IDateTimeProvider
{
   /// <summary> 
   /// Normally this is a pass-through to DateTime.Now, but it can be 
   /// overridden with SetDateTime( .. ) for testing or debugging.
   /// </summary>
   private Func<DateTime> _now = () => DateTime.Now;
   public Func<DateTime> Now { get => _now; private set => _now = value; }

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

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

I have chosen to make the setter private for Now(). As a result, a developer must explicitly use the method SetDateTime() to change the time. You can also choose to use a auto getter and setter property.

I also added an interface so that the class can be injected:

public interface IDateTimeProvider
{
    Func<DateTime> Now { get; }
    void SetDateTime(DateTime dateTimeNow);
    void ResetDateTime();
}

I hope that someone else will also benefit from this.

Isolating Code Under Test with Microsoft Fakes, A stub replaces a class with a small substitute that implements the same interface . A shim modifies the compiled code of your application at run time so that instead NET Core and SDK-style project support is currently in preview. Shims can replace calls to private methods if all the types on the method� 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.

Fakes alternative in Visual studio 2017 - MSDN, 2. if no, can you please share any Fakes alternative in V2017 for this (with in Microsoft using Nuget or etc.). 3. Is there Michael Taylor http://www. michaeltaylorp3.net. Thursday Friday, September 21, 2018 5:51 AM. Reply. Add Fakes Assembly. In Solution Explorer, open your unit test project's references and select the reference to the assembly that contains the method you want to fake. In this example, the DateTime class is in System.dll. To see the references in a Visual Basic project, choose Show All Files. Choose Add Fakes Assembly. Insert a shim in a

Best practices for writing unit tests, A mock starts out as a Fake until it's asserted against. Stub - A stub is a controllable replacement for an existing dependency (or collaborator) in� You can specify a different public key for the Fakes assembly, such as a key you have created for the shimmed assembly, by specifying the full path to the.snk file that contains the alternate key as the KeyFile attribute value in the Fakes \ Compilation element of the.fakes file.

Visual Studio 2019 and .NET Core Fakes!, Visual Studio 2019, Preview 16.6 is now available with great new features. One of the features included in this preview is the support for Microsoft Fakes in . NET Core. This blog is designed to show how you can take advantage of this feature. Fake - A fake is a generic term that can be used to describe either a stub or a mock object. Whether it's a stub or a mock depends on the context in which it's used. So in other words, a fake can be a stub or a mock. Mock - A mock object is a fake object in the system that decides whether or not a unit test has passed or failed. A mock starts

Comments
  • It is very good solution. You will need this mocking also in manual testing or event in production, when you will need have current time different than time which is set up in OS.
  • You are actually improving the implementation by making the dependency on DateTime.Now explicit (which I assume was a hidden dependency)
  • The proper way, for any system, platform, programming language, etc. is to actually remove the hard dependency on DateTime.Now (or the equivalent if this is another system, platform, etc.) so that you can inject it. Hidden dependencies are the scourge of testing because ... well ... they are hidden, ie. not visible to you, so they're just too darn easy to miss and overlook when you set up new test scenarios. For you to actually put it behind an abstraction that then has to be injected (which you should also do) makes this dependency visible, and thus easier to handle.
  • Rather than use a static, I would (like Lasse) generally implement this as a DateTimeProvider : IDateTimeProvider. Then your classes have an explicit dependency on IDateTimeProvider. This makes it crystal clear what classes have a dependency on DateTime, and thus need to be mocked. The downside of using static, from a unit testing standpoint, is that your tests then can't be parallelised (since there is only a single static value, so if different tests want to model different values they will trample on each other).
  • It's on their roadmap. See developercommunity.visualstudio.com/idea/351467/….
  • It seems to bomb whenever there is a call to an Async method, as well. github.com/tonerdo/pose/issues/25
  • Nice try, but this is not a replacement to what Fakes did. It would not affect any 3rd party lib calls working with time and requires every team member to learn and consistently use the replacement API (like that's gonna happen) I do miss my fakes, they just worked with no fuss :(
  • There's an interface called ISystemClock which is meant for this purpose of mocking out the current datetime.
  • @ImrePühvel - if you miss Fakes that much, check out the nuget package 'Pose'.
  • @ChrisGessler, I did. Welcome try, but last time I checked, it it did not handle all my cases and seemed not production-ready yet. Probably works for DateTime.Now, though.
  • Pose also hasn't been updated since 6/2018 and I can't seem to get it to work with .NET Core 3.1 at all. I get InvalidProgramExceptions when I try to call the Isolate method.