views:

69

answers:

1

We have a DateTime Service that pulls the Database date down when we need to use a Date. Now, If 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!

+1  A: 

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.

Aaron Digulla
So if I was testing a control that called this class from inside of it (thus not being able to pass in the DateTime value), how would I get the new value into it? If I create a new class and inherit, the control would still be calling X?
James Armstead
You would rewrite your control to allow you to pass in an instance of X. From the TDD lore: Static methods and internal calls to `new` can't be tested. You must wrap them with a factory method.
Aaron Digulla