3

I'm working on a dotnet core project, trying to mock some third party classes in my Xunit.net tests. The class I'm trying to fake is not mockable through constrained frameworks like Moq or NSubstitute. So I need an unconstrained framework to do that.

Say I want to fake DateTime.Now in my .net core test projects.

In .net 4.5 we have MSFakes(Moles), Smocks, etc. None of them support dotnet core framework yet. Shim is not ported to dotnet core yet.

Anyone knows any isolation framework or technique that will achieve my goal for .NET Core at the present time?

akardon
  • 43,164
  • 4
  • 34
  • 42
  • Did you find a isolation solution for dotnet core? – ahong Dec 31 '20 at 06:19
  • 1
    Didn't find it at the time, so I did some workarounds for that: https://codopia.wordpress.com/2017/04/24/how-to-mock-up-datetime-now-in-unit-tests-using-ambient-context-pattern/ But seemingly "JustMock" supports dotnet core now. Haven't tried it though. https://www.telerik.com/blogs/justmock-r3-2019-with-support-for-.net-core-3.0-and-more-features-for-mocking-non-public-api – akardon Jan 04 '21 at 03:33
  • Thanks... seems like a lot of the solutions out there are not free or use DI/some wrapper. – ahong Jan 04 '21 at 06:36
  • 1
    No worries. yeah and I guess the main issue for them is to get the framework working on all platforms (Linux, Win, Mac) with shimming. so maybe safer to just wrap those stuff in your own classes and mock them rather than relying on those frameworks. That's what I did. – akardon Jan 04 '21 at 07:03

3 Answers3

2

You could try a different way: wrap those third party libs with your own interfaces and wrapper classes. Then use a dummy object implementing that interface.

Things become more complicated with static properties like DateTime.Now. But it works similar: define an ITimeProvider interface with a Now property. Add a DateTimeWrapper : ITimeProvider with a public DateTime Now => DateTime.Now;, and - for convenience, but not required - a static class around it, e.g.

static class MyClock 
{ 
    static ITimeProvider _Instance; 
    static void SetInstanceForTesting(ITimeProvider instance) 
    { _Instance = instance; }
    static DateTime Now => _Instance.Now; 
}

Alternatively, you may inject an ITimeProvider instance to the objects needing it.

Bernhard Hiller
  • 2,163
  • 2
  • 18
  • 33
  • Yeah, I've done so for now, but the main problem still exists. Now the question is what if I want to write a test for the `DateTimeWrapper`. If someone goes and changes that class and breaks the code how would we know about the breaking change. – akardon Nov 30 '16 at 22:38
1

A somewhat late reply, but for anyone else looking after the same kind of functionality I would recommend the open source git project prig: https://github.com/urasandesu/Prig It can fake static methods (they even have an example of faking and isolating DateTime.Now) and is an alternative to Microsoft Fakes, TypeMock and JustMock (all who cost money or require ultimate editions of visual studio to use).

Anders Asplund
  • 575
  • 2
  • 7
  • 1
    Hmm, now that you say it, it probably don't. What it basically do is that it replaces one binary file (like mscorlib.dll) to another file, then redirects the calls (like DateTime.Now) to a faked one, where you control what should happen and be returned. However it uses ATL and powershell, so probably not compatible with dotnet core. – Anders Asplund May 26 '17 at 02:35
1

Pose is an option. It's free full in memory Https://GitHub.com/tonerdo/pose

AllSpark
  • 425
  • 1
  • 4
  • 17