tonerdo / pose

Replace any .NET method (including static and non-virtual) with a delegate
MIT License
1.07k stars 75 forks source link

DateTime.Now returning incorrect value #38

Open BermudaLamb opened 5 years ago

BermudaLamb commented 5 years ago
        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

devedse commented 5 years ago

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.

BermudaLamb commented 5 years ago

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"

Tarig0 commented 5 years ago

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.