I need this in few of my automation test cases. The major functionality is time dependent and this causes test cases to fail if I run them during the day time for which there’s no record.
I know I have another but relatively tedious option of mocking the repository and generate the time dependent data at runtime, but – I just want to know whether I can do this in other way – by controling the DateTime for an application and/or thread such that DateTime.Now returns the value based on what I set somewhere at the start?
Is it really possible?
TypeMock isolator claims to be able to do this. Personally, though, I would rather change my code to inject an
IClockand use that. The production clock might useDateTime.Now(orUtcNow), but I can provide a mock simply and cleanly.If that affects too many things, maybe a static implementation, so you can use
Current.Timeor something (whereCurrentis your class), but which allows indirection / spoofing for tests. Indeed, that could be done using the above, simply with: