Static DateTime for testing
We have a DateTime Service that pulls the Database date down when we need to use a Date. Now, If开发者_StackOverflow社区 I want to run a test using "Todays" Date using FitNesse, What would be the easiest way of creating a way to have (OurService).CurrentDate be static?
We have a control that ads Days,Years, or Months to the current date.. and I would like to be able to test this.
Thanks!
In your code, add a method to pull the date from "somewhere" ("somewhere" is an implementation detail). For example, in my code:
public class X {
Date now = new Date ();
}
is replaced with:
public class X {
Date now = now ();
protected Date now () { return new Date (); }
}
I can now extend X in my tests to override now()
with something that returns a fixed date.
Also add a test which calls the original implementation but which ignores the result (well, you can check that the result is within a range of "now"). This test just makes sure that the old date pulling code continues to work.
精彩评论