Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shim for DateTime.UtcNow not working #67

Open
Driedas opened this issue Oct 7, 2021 · 5 comments · May be fixed by Miista/pose#29
Open

shim for DateTime.UtcNow not working #67

Driedas opened this issue Oct 7, 2021 · 5 comments · May be fixed by Miista/pose#29

Comments

@Driedas
Copy link

Driedas commented Oct 7, 2021

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
Copy link

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

@Driedas
Copy link
Author

Driedas commented Dec 8, 2021

@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
Copy link

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
Copy link

Miista commented Jan 13, 2024

@Driedas @princefishthrower Please see my comment on #63

Miista added a commit to Miista/pose that referenced this issue Jan 25, 2024
Release v2.0 to NuGet

Version 2.0 supports the following targets:
* .NET Standard 2.0
* .NET Core 2.0
* .NET Core 3.0
* .NET Framework 4.8
* .NET 7
* .NET 8

Version 2.0 fixes the following issues:
* tonerdo/pose#17
* tonerdo/pose#37
* tonerdo/pose#38
* tonerdo/pose#41
* tonerdo/pose#47
* tonerdo/pose#49
* tonerdo/pose#60
* tonerdo/pose#67
* tonerdo/pose#68
* tonerdo/pose#70
* tonerdo/pose#71
* tonerdo/pose#72
* tonerdo/pose#75
* tonerdo/pose#79
@Miista
Copy link

Miista commented Jan 27, 2024

@princefishthrower For support for async/await please see Miista/pose#12

@Miista Miista linked a pull request Jan 28, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants