DateTime.Now returning incorrect value
var consoleShim = Shim.Replace(() => Console.WriteLine(Pose.Is.A<string>())).With(
delegate (string s) { Console.WriteLine("Hijacked: {0}", s); });
var shimDate = new DateTime(2014, 4, 4);
Console.WriteLine(shimDate);
var dateTimeShim = Shim.Replace(() => DateTime.Now).With(() => shimDate);
PoseContext.Isolate(() =>
{
Console.WriteLine("Hello World!");
var dt = DateTime.Now;
Console.WriteLine(dt);
Console.WriteLine(DateTime.Now);
}, consoleShim, dateTimeShim);
Test is returning: 4/18/D1935668524 3:40:45 AM
You're mocking Console.WriteLine(string). However you're providing a DateTime to the Console.WriteLine class. I expect this will call Console.WriteLine(object). It basically means you have to mock that one.
This is a near direct copy of the README example on the primary page .. According to that:
// Outputs "Hijacked: Hello World!"
Console.WriteLine("Hello World!");
// Outputs "4/4/04 12:00:00 AM"
Console.WriteLine(DateTime.Now);
Even though it is within my PoseContext.Isolate() the string writing is working as expected. However, the shim for the DateTime is returning a junk date, rather than the expected "4/4/04 12:00:00 AM"
what happens if you construct the datetime in the shim? I think there may be a memory access issue when you use a variable from outside a shim.