mardi 17 février 2015

How to write unit test when expect very long

I think the expected results are been a hard code but my expected results in my test are very long and duplicated pattern. and I have same case about 3 case.


I should write this expected results following this:



public function testConvertTargetByWeek()
{
$revenue = 100;
$week = 50;

$revenueByWeeks = convertRevenueByWeek($revenue, $week);

$expects = array(
1 => 2,
2 => 4,
...
50 => 100,
);

$this->assertEquals($expects, $revenueByWeeks);
}


or



public function testConvertTargetByWeek()
{
$revenue = 100;
$week = 50;

$revenueByWeeks = convertRevenueByWeek($revenue, $week);

for(range(1, 50) as $_week)
{
$expect[$week] = $revenue * ($_week / $week);
}

$this->assertEquals($expects, $revenueByWeeks);
}

Aucun commentaire:

Enregistrer un commentaire