akardon
akardon

Reputation: 45996

Unconstrained Isolation (mocking) framework for dotnet core

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?

Upvotes: 3

Views: 1477

Answers (4)

QUANG VU
QUANG VU

Reputation: 1

If you are searching for a free and powerful framework for unit testing on dotnet core, I couldn't find a project that met my needs. I created one myself. Please feel free to use it:

UnconstrainedMockiu on GitHub

Upvotes: 0

AllSpark
AllSpark

Reputation: 425

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

Upvotes: 1

Anders Asplund
Anders Asplund

Reputation: 575

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

Upvotes: 1

Bernhard Hiller
Bernhard Hiller

Reputation: 2397

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.

Upvotes: 2

Related Questions