Skip to content

DateTime.UtcNow.TimeOfDay always returns zero #48

@mamen

Description

@mamen

I am trying to move away from Fakes and stumbled upon this repository. I was able to successfully convert most of my tests which used Fakes with Pose(r). However, I was not able to convert some of the tests which use DateTime.

In one of the tests, the Shim is set up like this:

var dateTimeShim = Shim.Replace(() => DateTime.UtcNow)
                        .With(() => new DateTime(2017, 10, 6, 15, 16, 17, 745, DateTimeKind.Utc));
                                          
PoseContext.Isolate(() =>
{
    actual = _fixture.DoSomething();
}, dateTimeShim);

In the method under test, DateTime is accessed like this:

var now = DateTime.UtcNow;
return (long)now.TimeOfDay.TotalMilliseconds / 100;

If I run this test, the returned value for TimeOfDay is 00:00:00 and thus, TotalMilliseconds is also 0. However, when using Fakes, TimeOfDay returns 15:16:17.7450000 and TotalMilliseconds returns 54977745.

Am I doing something wrong or is this a (known) bug?

-- Edit --
I fixed it by introducing a second Shim for the TimeOfDay-property:

var dateTimeShim = Shim.Replace(() => DateTime.UtcNow).With(() => timestamp);
var timeOfDayShim = Shim.Replace(() => Is.A<DateTime>().TimeOfDay)
                        .With((ref DateTime @this) => timestamp.TimeOfDay);

Metadata

Metadata

Assignees

Labels

question/investigationFurther information or investigation is required

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions