Skip to content

Instantly share code, notes, and snippets.

@DavidArno
Created September 1, 2011 12:47
Show Gist options
  • Save DavidArno/1186095 to your computer and use it in GitHub Desktop.
Save DavidArno/1186095 to your computer and use it in GitHub Desktop.
Is this a code smell? Writing code to support testing
The following two methods use the currentDateTime() method to get the current time,
rather than calling new Date() directly. the only reason I did this was so that I
could override currentDateTime() in my test case class in order to pass in an
existing Date object to ensure that the time would not have changed between the
method being called and the assert made.
Is this bad practice? I can't decide.
/**
* Callback method used by _macroSubstitutions to provide the current date in DD_MMM_YYYY format
*/
protected function generateDate():String
{
var date:Date = currentDateTime;
var formatter:DateFormatter = new DateFormatter();
formatter.formatString = "DD_MMM_YYYY";
return formatter.format(date);
}
/**
* Callback method used by _macroSubstitutions to provide the current time in HH_MM format
*/
protected function generateTime():String
{
var date:Date = currentDateTime;
var formatter:DateFormatter = new DateFormatter();
formatter.formatString = "HH_NN";
return formatter.format(date);
}
protected function get currentDateTime():Date
{
return new Date();
}
@philipschwarz
Copy link

@xpmatteo
Copy link

xpmatteo commented Sep 8, 2011

Hi David,

the standard technique for doing this is to have a Clock service that is passed in as a collaborator. For instance

interface Clock { Date now(); }
class RealClock { 
    public Date now() { return new Date() }
}
class FakeClock {
    private Date toReturn;
    public FakeClock(Date toReturn) { this.toReturn = toReturn; }
    public Date now() { return toReturn; }
}
class MacroSubstitutions {
    // constructor that takes a Clock service, used by the tests
    public MacroSubstitutions(Clock clock) { this.clock = clock; }
    // default constructor, used in production code
    public MacroSubstitutions() { this(new RealClock()); }
    // ...
}

@jbrains
Copy link

jbrains commented Sep 8, 2011

Extract-and-override is almost always a step towards introducing a new Collaborator. See "Replace Inheritance with Delegation" in Fowler's Refactoring. Introducing the Collaborator inverts the dependency, increasing context independence by pushing dependency on the runtime environment up a level of the call stack. That all sounds good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment