pose icon indicating copy to clipboard operation
pose copied to clipboard

DateTime.Now returning incorrect value

Open BermudaLamb opened this issue 7 years ago • 3 comments

        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

BermudaLamb avatar Oct 11 '18 15:10 BermudaLamb

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.

devedse avatar Oct 11 '18 15:10 devedse

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"

BermudaLamb avatar Oct 11 '18 16:10 BermudaLamb

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.

Tarig0 avatar Oct 26 '18 12:10 Tarig0