Reputation: 11
I am writing a mock testing for a C# .NET Core version 8 project with MSTesting. The library uses static methods that I want to fake in my test project. I found online about using Moles (shims) with the following syntax:
using Microsoft.QualityTools.Testing.Fakes;
using (ShimsContext.Create())
{
System.Fakes.ShimDateTime.NowGet = () => new DateTime(1837, 1, 1);
^^^^^^^^^^^^
Error
}
However, I can't figure out (after reading the documentation and other posts) how to add Fakes to System.Runtime.
Many instruction says in Visual Studio, you do Add > Add Fakes to the project assembly, but no further instruction. But I don't find this option at all. How to use System.Fakes in .NET Core MSTest project to fake static methods, if there is no way, is there an alternative way to make a mock tests on this?
Upvotes: 1
Views: 344
Reputation: 22849
If memory serves well then Fakes was popular around a decade ago (Visual Studio 2012 and 2015 era).
Since then a lot of things happened to .Net and there are proprietary (like JustMock) and open-source alternatives (like Pose):
using Pose;
Shim dateTimeShim = Shim.Replace(() => DateTime.Now).With(() => new DateTime(2004, 4, 4));
It has limitations but I think it's worth a try. Also worth mentioning that JustMock have a free Lite edition as well.
Upvotes: 2