Today’s post goes rather quick … Telerik JustMock in its commercial (i.e. non-free) version provides you the option to mock static classes, which comes in really handy when you want […]
Today’s post goes rather quick … Telerik JustMock in its commercial (i.e. non-free) version provides you the option to mock static classes, which comes in really handy when you want to mock something like DateTimeOffset. However, in our scenario it yielded some surprising results…
In my project I needed to mock a DateTimeOffset to have a sut (subject under test) calculate some relative dates from Now. At the same time, I wanted to assure that DateTimeOffset was called in all instances and NOTDateTime. So I created this mock:
public void GettingNextScheduleForScheduledJobReturnsExpectedDate()
var now = DateTimeOffset.Parse("2019-04-13 08:15:42+02:00");
var expectedSchedule = DateTimeOffset.Parse("2019-04-13 18:00:00+02:00");
Mock.Arrange(() => DateTimeOffset.Now)
Mock.Arrange(() => DateTime.Now)
var job = new ScheduledJob()
Crontab = "* 18 * * *"
var sut = new ScheduledJobScheduler(job);
var result = sut.GetNextSchedule();
Mock.Assert(() => DateTime.Now);
Mock.Assert(() => DateTimeOffset.Now);
However the code in GetNextSchedule falsely returned DateTimeOffset.MinValue when calling DateTimeOffset.Now as you can see from this screenshot:
After some investigation I found out that the call to set up the static mock for DateTime was the cause of the problem:
As it seemed DateTimeOffset is internally relying on DateTime and the Behavior.Loose also applied to the constructor (as we did not specifically arrange that). So the solution was to simply delete that statement.
So in reality it was not really a problem of JustMock rather than using it in a wrong way. And with that we are at the end of our today’s story.
Leave a Reply