tonerdo / pose

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

shim for DateTime.UtcNow not working #67

Open Driedas opened 2 years ago

Driedas commented 2 years ago

Hi, I've stumbled upon this project looking for exactly this feature for unit testing DateTime.UtcNow (without having to resort to 3rd party libraries like NodaTime etc), have cloned the repository and added the following unit test, based on the documentation

[TestMethod]
public void TestReplacePropertyGetter2()
{
    PoseContext.Isolate(() =>
    {
        DateTime date = new DateTime(2000, 1, 1);

        Shim shim = Shim.Replace(() => DateTime.UtcNow)
            .With(() => date);

        Assert.AreEqual(date, DateTime.UtcNow);
    });
}

The result I get is a fail however, with the message

Assert.AreEqual failed. Expected:<01/01/2000 00:00:00>. Actual:<07/10/2021 13:03:16>. 

Am I doing something wrong here? The unit test suite that comes with the project is all green, however the above should work as well, according to my understanding...

MelbourneDeveloper commented 2 years ago

@Driedas I just realised that I posted exactly the same issue

Driedas commented 2 years ago

@MelbourneDeveloper yeah, probably going to get as much feedback as me :-)

Anyway, I've worked around it the same way I do usually, introduce an additional DateTime input parameter into the relevant method, which works just as well. I just wanted to give this a try when I saw it... There's other options, like introducing something like an IClock with a default implementation that just calls into DateTime.UtcNow

princefishthrower commented 2 years ago

Would love to see this resolved. To me, using a library like Pose is the correct way to test for DateTime (though likely against the thoughts of OOP purists). Feedback / community seems like a bit of a ghost town though, wonder where I can pitch in if at all...

There's also a general problem with async / await code that appears to be unresolved

Miista commented 5 months ago

@Driedas @princefishthrower Please see my comment on #63

Miista commented 5 months ago

@princefishthrower For support for async/await please see https://github.com/Miista/pose/issues/12