Unit testing date sensitive functions?
I am trying to write tests for a date helper class. I am wondering what is best practice for this.
Here is an example in PHP. public static function get_date($offset = 0){
$date = mktime(0, 0, 0, date('m') , date('d') + $offset, date('Y'));
return array(
'day' => date('D',$date),
'month' => date('M',$date),
'year' => date('Y',$date)
);
}
public static function today(){
return self::get_date();
}
public static function tomorrow(){
return self::get_date(1);
}
public static function yesterday(){
return self::get_date(-1);
}
So what I am looking for is examples of tests that could test these functions or a new way to write these functions so they are开发者_运维技巧 intuitively testable.
I have found examples in Java but they seem pretty inelegant and I really don't know Java, unless you count Actionscript 3 as Java ;)
Solutions in Javascript or Ruby would also be super helpful as examples but my class is written in PHP so that would be ideal.
Thanks!
I usually do something like this:
<?php
class Date
{
protected $date;
public function __construct($date = null)
{
$this->date = $date ? $date : time();
}
public function today()
{
// to something with $this->date
}
}
class DateTest extends PHPUnit_Framework_TestCase
{
public function testToday()
{
$date = new Date(strtotime('2010-10-17 00:00:00'));
$this->assertEquals($expectedValue, $date->today());
}
}
It follows the same principle that date()
does, which is to receive a second optional argument, the reference time.
精彩评论