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();
}
@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