Date override.

When we develop / QA, we often have to pretend that the current date / time is something different than now. For us, a fairly ordinary logic based on a date.

A simple example: let's say that the product in the store is available from December 15, 2010 to January 3, 2011. These two dates will be stored in the database, and the code will compare it with DateTime.Now. This obviously needs to be [unit] checked in some way. The question is how?

So far, we just added a custom object Dateand tried to force use Date.Nowinstead DateTime.Now, which does not cover any third-party code that can rely on the current date, and also feels too weak.

I wonder if there is a more kosher way to handle date overrides without changing the system date in Windows.

+3
source share
2 answers

For unit testing, I would recommend that you take a look at Microsoft Moles , an isolation environment that allows you to replace the call DateTime.Nowwith your own delegate, which can return any date you want.

+12
source

In pure .net there is no way, you have to call a function Win32SetSystemTimeusing pinvoke

-1
source

Source: https://habr.com/ru/post/1783415/


All Articles