-3

I made an app in C# and I want to test this app in specific times Day, Week, Year is there any free time emulation software that can do that or any other way?

Koopakiller
  • 2,838
  • 3
  • 32
  • 47
  • 1
    What is preventing you from changing the date on your computer? – entropic Aug 25 '15 at 19:04
  • 5
    Ideally the parts which are date/time-specific can be abstracted into testable units with the date/time as a supplied dependency. Then in your unit tests you can supply any date/time you want. – David Aug 25 '15 at 19:04
  • Can you post a sample of what is date-dependent? Without seeing any code, both entropic and David's comments should be more than suitable solutions. – sab669 Aug 25 '15 at 19:08
  • 1
    There's a good explanation in [Unit Testing: DateTime.Now](http://stackoverflow.com/a/2425739) of how to do this with dependency injection. – Joel V. Earnest-DeYoung Aug 25 '15 at 19:13

1 Answers1

6

It sounds like your code is tightly coupled with current time.

If you're directly calling, DateTime.Now, for instance, you could promote that value into a parameter in your method signature.

So instead of what you would have today (assumption):

public void DoSomething()
{
   var dateToTest = DateTime.Now;
   ProcessTime(time);
}

...your refactored code would look like:

public void DoSomething(DateTime dateToTest)
{
   ...
   ProcessTime(dateToTest);
}

By promoting the variable into the signature, you are creating a testable unit of code that doesn't depend on outside factors, such as dates or times.

A key factor in these situations is to separate integration testing from unit testing. Know that you can supply certain values under certain conditions, and given your assertions, know that your code will act in a predictable manner every time. In your case, it will act in a predictable manner every time, regardless of the real date and time.

George Johnston
  • 31,652
  • 27
  • 127
  • 172