4
\$\begingroup\$

This is the first time I have written Unit Tests. I am testing code that I have already written and would like to move to TDD once I have managed to get units written for everything that is already in my application.

Just wanted to be sure that the tests that I have written look good, or if there is a different approach I should be taken. These are for CakePHP 2.0

<?php
App::uses('Player', 'Model');

class PlayerTestCase extends CakeTestCase {
public $fixtures = array('app.player', 'app.club', 'app.position', 'app.stat', 'app.selection', 'app.period', 'app.league', 'app.draft', 'app.team');

public function setup() {
    parent::setUp();
    $this->Player = ClassRegistry::init('Player');
}

/**
 * testGetPlayers method
 *
 * @return void
 */ 
function testGetPlayers() {

    //Specific Player, All Stats, No Position Exclusion 
    $result = $this->Player->GetPlayers(11, 'all', null);
    $expected = array(
            'Player' => array ('id' => 11, 'name' => 'Forward 2', 'status' => 'doubtful', 'note' => '', 'club_id' => '2', 'position_id' => '4'),
            'Club' => array ('id' => 2, 'name' => 'Club 2', 'abbreviation' => 'clb2'),
            'Position' => array ('id' => 4, 'name' => 'Forward', 'abbreviation' => 'fw', 'min' => 1, 'max' => 3, 'need' => 2, 'db_abbr' => 'fw'),
            'Stat' => array (
                array ('id' => 11, 'rank' => 11, 'goals' => 0, 'saves' => 0, 'penaltysaves' => 0, 'assists' => 0, 'cleansheet' => 0, 'redcard' => 0, 'yellowcard' => 0, 'foulsconc' => 0, 'foulswon' => 0, 'tackleslost' => 0, 'tackleswon' => 0, 'shotontarget' => 0, 'totalshot' => 0, 'appearances' => 0, 'attcreated' => 0, 'totalpasses' => 0, 'accpasses' => 0, 'minutesplayed' => 0, 'conceded' => 0, 'player_id' => '11', 'team_id' => 0, 'period_id' => '1'),
                array ('id' => 23, 'rank' => 11, 'goals' => 0, 'saves' => 0, 'penaltysaves' => 0, 'assists' => 0, 'cleansheet' => 0, 'redcard' => 0, 'yellowcard' => 0, 'foulsconc' => 0, 'foulswon' => 0, 'tackleslost' => 0, 'tackleswon' => 0, 'shotontarget' => 0, 'totalshot' => 0, 'appearances' => 0, 'attcreated' => 0, 'totalpasses' => 0, 'accpasses' => 0, 'minutesplayed' => 0, 'conceded' => 0, 'player_id' => '11', 'team_id' => 0, 'period_id' => '2')
            )
    );
    $this->assertEquals($expected, $result);    

    //Specific Player, No Stats, No Position Exclusion
    $result = $this->Player->GetPlayers(10, false, null);       
    $expected = array(
            'Player' => array ('id' => 10, 'name' => 'Forward 1', 'status' => 'available', 'note' => '', 'club_id' => '1', 'position_id' => '4'),
            'Club' => array ('id' => 1, 'name' => 'Club 1', 'abbreviation' => 'clb1'),
            'Position' => array ('id' => 4, 'name' => 'Forward', 'abbreviation' => 'fw', 'min' => 1, 'max' => 3, 'need' => 2, 'db_abbr' => 'fw')
    );
    $this->assertEquals($expected, $result);    

    //No Specific Player, No Stats, Position Exclusion (Array)
    $result = $this->Player->GetPlayers(null, false, array('def', 'mid', 'fw'));        
    $expected = array(
        array(
            'Player' => array ('id' => 1, 'name' => 'Goalkeeper 1', 'status' => 'available', 'note' => '', 'club_id' => '1', 'position_id' => '1'),
            'Club' => array ('id' => 1, 'name' => 'Club 1', 'abbreviation' => 'clb1'),
            'Position' => array ('id' => 1, 'name' => 'Goalkeeper', 'abbreviation' => 'gk', 'min' => 1, 'max' => 1, 'need' => 1, 'db_abbr' => 'gk')
            ),
        array(
            'Player' => array ('id' => 2, 'name' => 'Goalkeeper 2', 'status' => 'doubtful', 'note' => '', 'club_id' => '2', 'position_id' => '1'),
            'Club' => array ('id' => 2, 'name' => 'Club 2', 'abbreviation' => 'clb2'),
            'Position' => array ('id' => 1, 'name' => 'Goalkeeper', 'abbreviation' => 'gk', 'min' => 1, 'max' => 1, 'need' => 1, 'db_abbr' => 'gk')
            ),
        array(
            'Player' => array ('id' => 3, 'name' => 'Goalkeeper 3', 'status' => 'unavailable', 'note' => '', 'club_id' => '3', 'position_id' => '1'),
            'Club' => array ('id' => 3, 'name' => 'Club 3', 'abbreviation' => 'clb3'),
            'Position' => array ('id' => 1, 'name' => 'Goalkeeper', 'abbreviation' => 'gk', 'min' => 1, 'max' => 1, 'need' => 1, 'db_abbr' => 'gk')
            )
    );
    $this->assertEquals($expected, $result);    

    //No Specific Player, Default Stats, Position Exclusion (String)
    $result = $this->Player->GetPlayers(null, true, 'gk');      
    $expected = array(
        array(
            'Player' => array ('id' => 4, 'name' => 'Defender 1', 'status' => 'available', 'note' => '', 'club_id' => '1', 'position_id' => '2'),
            'Club' => array ('id' => 1, 'name' => 'Club 1', 'abbreviation' => 'clb1'),
            'Position' => array ('id' => 2, 'name' => 'Defender', 'abbreviation' => 'def', 'min' => 3, 'max' => 5, 'need' => 4, 'db_abbr' => 'def'),
            'Stat' => array ('id' => 4, 'rank' => 4, 'goals' => 0, 'saves' => 0, 'penaltysaves' => 0, 'assists' => 0, 'cleansheet' => 0, 'redcard' => 0, 'yellowcard' => 0, 'foulsconc' => 0, 'foulswon' => 0, 'tackleslost' => 0, 'tackleswon' => 0, 'shotontarget' => 0, 'totalshot' => 0, 'appearances' => 0, 'attcreated' => 0, 'totalpasses' => 0, 'accpasses' => 0, 'minutesplayed' => 0, 'conceded' => 0, 'player_id' => '4', 'team_id' => 0, 'period_id' => '1')
            ),
        array(
            'Player' => array ('id' => 5, 'name' => 'Defender 2', 'status' => 'doubtful', 'note' => '', 'club_id' => '2', 'position_id' => '2'),
            'Club' => array ('id' => 2, 'name' => 'Club 2', 'abbreviation' => 'clb2'),
            'Position' => array ('id' => 2, 'name' => 'Defender', 'abbreviation' => 'def', 'min' => 3, 'max' => 5, 'need' => 4, 'db_abbr' => 'def'),
            'Stat' => array ('id' => 5, 'rank' => 5, 'goals' => 0, 'saves' => 0, 'penaltysaves' => 0, 'assists' => 0, 'cleansheet' => 0, 'redcard' => 0, 'yellowcard' => 0, 'foulsconc' => 0, 'foulswon' => 0, 'tackleslost' => 0, 'tackleswon' => 0, 'shotontarget' => 0, 'totalshot' => 0, 'appearances' => 0, 'attcreated' => 0, 'totalpasses' => 0, 'accpasses' => 0, 'minutesplayed' => 0, 'conceded' => 0, 'player_id' => '5', 'team_id' => 0, 'period_id' => '1')
            ),
        array(
            'Player' => array ('id' => 6, 'name' => 'Defender 3', 'status' => 'unavailable', 'note' => '', 'club_id' => '3', 'position_id' => '2'),
            'Club' => array ('id' => 3, 'name' => 'Club 3', 'abbreviation' => 'clb3'),
            'Position' => array ('id' => 2, 'name' => 'Defender', 'abbreviation' => 'def', 'min' => 3, 'max' => 5, 'need' => 4, 'db_abbr' => 'def'),
            'Stat' => array ('id' => 6, 'rank' => 6, 'goals' => 0, 'saves' => 0, 'penaltysaves' => 0, 'assists' => 0, 'cleansheet' => 0, 'redcard' => 0, 'yellowcard' => 0, 'foulsconc' => 0, 'foulswon' => 0, 'tackleslost' => 0, 'tackleswon' => 0, 'shotontarget' => 0, 'totalshot' => 0, 'appearances' => 0, 'attcreated' => 0, 'totalpasses' => 0, 'accpasses' => 0, 'minutesplayed' => 0, 'conceded' => 0, 'player_id' => '6', 'team_id' => 0, 'period_id' => '1')
            ),
        array(
            'Player' => array ('id' => 7, 'name' => 'Midfielder 1', 'status' => 'available', 'note' => '', 'club_id' => '1', 'position_id' => '3'),
            'Club' => array ('id' => 1, 'name' => 'Club 1', 'abbreviation' => 'clb1'),
            'Position' => array ('id' => 3, 'name' => 'Midfielder', 'abbreviation' => 'mid', 'min' => 2, 'max' => 5, 'need' => 4, 'db_abbr' => 'mid'),
            'Stat' => array ('id' => 7, 'rank' => 7, 'goals' => 0, 'saves' => 0, 'penaltysaves' => 0, 'assists' => 0, 'cleansheet' => 0, 'redcard' => 0, 'yellowcard' => 0, 'foulsconc' => 0, 'foulswon' => 0, 'tackleslost' => 0, 'tackleswon' => 0, 'shotontarget' => 0, 'totalshot' => 0, 'appearances' => 0, 'attcreated' => 0, 'totalpasses' => 0, 'accpasses' => 0, 'minutesplayed' => 0, 'conceded' => 0, 'player_id' => '7', 'team_id' => 0, 'period_id' => '1')
            ),
        array(
            'Player' => array ('id' => 8, 'name' => 'Midfielder 2', 'status' => 'doubtful', 'note' => '', 'club_id' => '2', 'position_id' => '3'),
            'Club' => array ('id' => 2, 'name' => 'Club 2', 'abbreviation' => 'clb2'),
            'Position' => array ('id' => 3, 'name' => 'Midfielder', 'abbreviation' => 'mid', 'min' => 2, 'max' => 5, 'need' => 4, 'db_abbr' => 'mid'),
            'Stat' => array ('id' => 8, 'rank' => 8, 'goals' => 0, 'saves' => 0, 'penaltysaves' => 0, 'assists' => 0, 'cleansheet' => 0, 'redcard' => 0, 'yellowcard' => 0, 'foulsconc' => 0, 'foulswon' => 0, 'tackleslost' => 0, 'tackleswon' => 0, 'shotontarget' => 0, 'totalshot' => 0, 'appearances' => 0, 'attcreated' => 0, 'totalpasses' => 0, 'accpasses' => 0, 'minutesplayed' => 0, 'conceded' => 0, 'player_id' => '8', 'team_id' => 0, 'period_id' => '1')
            ),
        array(
            'Player' => array ('id' => 9, 'name' => 'Midfielder 3', 'status' => 'unavailable', 'note' => '', 'club_id' => '3', 'position_id' => '3'),
            'Club' => array ('id' => 3, 'name' => 'Club 3', 'abbreviation' => 'clb3'),
            'Position' => array ('id' => 3, 'name' => 'Midfielder', 'abbreviation' => 'mid', 'min' => 2, 'max' => 5, 'need' => 4, 'db_abbr' => 'mid'),
            'Stat' => array ('id' => 9, 'rank' => 9, 'goals' => 0, 'saves' => 0, 'penaltysaves' => 0, 'assists' => 0, 'cleansheet' => 0, 'redcard' => 0, 'yellowcard' => 0, 'foulsconc' => 0, 'foulswon' => 0, 'tackleslost' => 0, 'tackleswon' => 0, 'shotontarget' => 0, 'totalshot' => 0, 'appearances' => 0, 'attcreated' => 0, 'totalpasses' => 0, 'accpasses' => 0, 'minutesplayed' => 0, 'conceded' => 0, 'player_id' => '9', 'team_id' => 0, 'period_id' => '1')
            ),
        array(
            'Player' => array ('id' => 10, 'name' => 'Forward 1', 'status' => 'available', 'note' => '', 'club_id' => '1', 'position_id' => '4'),
            'Club' => array ('id' => 1, 'name' => 'Club 1', 'abbreviation' => 'clb1'),
            'Position' => array ('id' => 4, 'name' => 'Forward', 'abbreviation' => 'fw', 'min' => 1, 'max' => 3, 'need' => 2, 'db_abbr' => 'fw'),
            'Stat' => array ('id' => 10, 'rank' => 10, 'goals' => 0, 'saves' => 0, 'penaltysaves' => 0, 'assists' => 0, 'cleansheet' => 0, 'redcard' => 0, 'yellowcard' => 0, 'foulsconc' => 0, 'foulswon' => 0, 'tackleslost' => 0, 'tackleswon' => 0, 'shotontarget' => 0, 'totalshot' => 0, 'appearances' => 0, 'attcreated' => 0, 'totalpasses' => 0, 'accpasses' => 0, 'minutesplayed' => 0, 'conceded' => 0, 'player_id' => '10', 'team_id' => 0, 'period_id' => '1')
            ),
        array(
            'Player' => array ('id' => 11, 'name' => 'Forward 2', 'status' => 'doubtful', 'note' => '', 'club_id' => '2', 'position_id' => '4'),
            'Club' => array ('id' => 2, 'name' => 'Club 2', 'abbreviation' => 'clb2'),
            'Position' => array ('id' => 4, 'name' => 'Forward', 'abbreviation' => 'fw', 'min' => 1, 'max' => 3, 'need' => 2, 'db_abbr' => 'fw'),
            'Stat' => array ('id' => 11, 'rank' => 11, 'goals' => 0, 'saves' => 0, 'penaltysaves' => 0, 'assists' => 0, 'cleansheet' => 0, 'redcard' => 0, 'yellowcard' => 0, 'foulsconc' => 0, 'foulswon' => 0, 'tackleslost' => 0, 'tackleswon' => 0, 'shotontarget' => 0, 'totalshot' => 0, 'appearances' => 0, 'attcreated' => 0, 'totalpasses' => 0, 'accpasses' => 0, 'minutesplayed' => 0, 'conceded' => 0, 'player_id' => '11', 'team_id' => 0, 'period_id' => '1')
            ),
        array(
            'Player' => array ('id' => 12, 'name' => 'Forward 3', 'status' => 'unavailable', 'note' => '', 'club_id' => '3', 'position_id' => '4'),
            'Club' => array ('id' => 3, 'name' => 'Club 3', 'abbreviation' => 'clb3'),
            'Position' => array ('id' => 4, 'name' => 'Forward', 'abbreviation' => 'fw', 'min' => 1, 'max' => 3, 'need' => 2, 'db_abbr' => 'fw'),
            'Stat' => array ('id' => 12, 'rank' => 12, 'goals' => 0, 'saves' => 0, 'penaltysaves' => 0, 'assists' => 0, 'cleansheet' => 0, 'redcard' => 0, 'yellowcard' => 0, 'foulsconc' => 0, 'foulswon' => 0, 'tackleslost' => 0, 'tackleswon' => 0, 'shotontarget' => 0, 'totalshot' => 0, 'appearances' => 0, 'attcreated' => 0, 'totalpasses' => 0, 'accpasses' => 0, 'minutesplayed' => 0, 'conceded' => 0, 'player_id' => '12', 'team_id' => 0, 'period_id' => '1')
            )
    );
    $this->assertEquals($expected, $result);    

    //Specific Player, Default Stats, No Position Exclusion
    $result = $this->Player->GetPlayers(10, true, null);        
    $expected = array(
            'Player' => array ('id' => 10, 'name' => 'Forward 1', 'status' => 'available', 'note' => '', 'club_id' => '1', 'position_id' => '4'),
            'Club' => array ('id' => 1, 'name' => 'Club 1', 'abbreviation' => 'clb1'),
            'Position' => array ('id' => 4, 'name' => 'Forward', 'abbreviation' => 'fw', 'min' => 1, 'max' => 3, 'need' => 2, 'db_abbr' => 'fw'),
            'Stat' => array ('id' => 10, 'rank' => 10, 'goals' => 0, 'saves' => 0, 'penaltysaves' => 0, 'assists' => 0, 'cleansheet' => 0, 'redcard' => 0, 'yellowcard' => 0, 'foulsconc' => 0, 'foulswon' => 0, 'tackleslost' => 0, 'tackleswon' => 0, 'shotontarget' => 0, 'totalshot' => 0, 'appearances' => 0, 'attcreated' => 0, 'totalpasses' => 0, 'accpasses' => 0, 'minutesplayed' => 0, 'conceded' => 0, 'player_id' => '10', 'team_id' => 0, 'period_id' => '1')
    );
    $this->assertEquals($expected, $result);    

    //Specific Player, Specific Stats, No Position Exclusion
    $result = $this->Player->GetPlayers(10, 2, null);       
    $expected = array(
            'Player' => array ('id' => 10, 'name' => 'Forward 1', 'status' => 'available', 'note' => '', 'club_id' => '1', 'position_id' => '4'),
            'Club' => array ('id' => 1, 'name' => 'Club 1', 'abbreviation' => 'clb1'),
            'Position' => array ('id' => 4, 'name' => 'Forward', 'abbreviation' => 'fw', 'min' => 1, 'max' => 3, 'need' => 2, 'db_abbr' => 'fw'),
            'Stat' => array ('id' => 22, 'rank' => 10, 'goals' => 0, 'saves' => 0, 'penaltysaves' => 0, 'assists' => 0, 'cleansheet' => 0, 'redcard' => 0, 'yellowcard' => 0, 'foulsconc' => 0, 'foulswon' => 0, 'tackleslost' => 0, 'tackleswon' => 0, 'shotontarget' => 0, 'totalshot' => 0, 'appearances' => 0, 'attcreated' => 0, 'totalpasses' => 0, 'accpasses' => 0, 'minutesplayed' => 0, 'conceded' => 0, 'player_id' => '10', 'team_id' => 0, 'period_id' => '2')
    );
    $this->assertEquals($expected, $result);

    //Player That Doesn't Exist
    $result = $this->Player->GetPlayers(999999, true, null);
    $this->assertFalse($result);        

    //Stats That Don't Exist
    $result = $this->Player->GetPlayers(null, 9999999, null);
    $this->assertFalse($result);    

    //Filter All Positions
    $result = $this->Player->GetPlayers(null, 9999999, array('gk', 'def', 'mid', 'fw'));
    $this->assertFalse($result);

}

//GetAvailablePlayers($league_id = null, $get_stats = true, $top_ranked_only = false, $exclude_positions = null) {  

function testGetAvailablePlayers() {

    //Get The Top Ranked Available Player with Stats
    $result = $this->Player->GetAvailablePlayers(1, null, true, null);
    $expected = array(
        'Player' => array ('id' => 2, 'name' => 'Goalkeeper 2', 'status' => 'doubtful', 'note' => '', 'club_id' => '2', 'position_id' => '1'),
        'Club' => array ('id' => 2, 'name' => 'Club 2', 'abbreviation' => 'clb2'),
        'Position' => array ('id' => 1, 'name' => 'Goalkeeper', 'abbreviation' => 'gk', 'min' => 1, 'max' => 1, 'need' => 1, 'db_abbr' => 'gk'),
        'Stat' => array ('id' => 2, 'rank' => 2, 'goals' => 0, 'saves' => 0, 'penaltysaves' => 0, 'assists' => 0, 'cleansheet' => 0, 'redcard' => 0, 'yellowcard' => 0, 'foulsconc' => 0, 'foulswon' => 0, 'tackleslost' => 0, 'tackleswon' => 0, 'shotontarget' => 0, 'totalshot' => 0, 'appearances' => 0, 'attcreated' => 0, 'totalpasses' => 0, 'accpasses' => 0, 'minutesplayed' => 0, 'conceded' => 0, 'player_id' => '2', 'team_id' => 0, 'period_id' => '1'),
        'Selection' => array ('id' => '', 'drafted' => '', 'locked' => '', 'position_id' => '', 'league_id' => '', 'team_id' => '', 'player_id' => '', 'period_id' => '')
    );
    $this->assertEquals($expected, $result);

    //Get All Available Players with No Stats Filtered by Position
    $result = $this->Player->GetAvailablePlayers(1, false, null, array('def', 'mid', 'fw'));
    $expected = array(
        array( 
        'Player' => array ('id' => 2, 'name' => 'Goalkeeper 2', 'status' => 'doubtful', 'note' => '', 'club_id' => '2', 'position_id' => '1'),
        'Club' => array ('id' => 2, 'name' => 'Club 2', 'abbreviation' => 'clb2'),
        'Position' => array ('id' => 1, 'name' => 'Goalkeeper', 'abbreviation' => 'gk', 'min' => 1, 'max' => 1, 'need' => 1, 'db_abbr' => 'gk'),
        'Selection' => array ('id' => '', 'drafted' => '', 'locked' => '', 'position_id' => '', 'league_id' => '', 'team_id' => '', 'player_id' => '', 'period_id' => '')
        ), 
        array( 
        'Player' => array ('id' => 3, 'name' => 'Goalkeeper 3', 'status' => 'unavailable', 'note' => '', 'club_id' => '3', 'position_id' => '1'),
        'Club' => array ('id' => 3, 'name' => 'Club 3', 'abbreviation' => 'clb3'),
        'Position' => array ('id' => 1, 'name' => 'Goalkeeper', 'abbreviation' => 'gk', 'min' => 1, 'max' => 1, 'need' => 1, 'db_abbr' => 'gk'),
        'Selection' => array ('id' => '', 'drafted' => '', 'locked' => '', 'position_id' => '', 'league_id' => '', 'team_id' => '', 'player_id' => '', 'period_id' => '')
        ), 
    );
    $this->assertEquals($expected, $result);

    //Test With League That Doesn't Exist
    $result = $this->Player->GetAvailablePlayers(5);
    $this->assertFalse($result);

}

function testIsAvailable() { 
    $result = $this->Player->IsAvailable(1, 1);
    $this->assertFalse($result);

    $result = $this->Player->IsAvailable(2, 1);
    $this->assertTrue($result); 

    $result = $this->Player->IsAvailable(1, 999999);
    $this->assertFalse($result);        
}

}
?>
\$\endgroup\$
  • \$\begingroup\$ Where does $this->Player->GetPlayers(11, 'all', null); get its data from in this test? \$\endgroup\$ – User Oct 31 '11 at 17:35
  • \$\begingroup\$ It's a method in my Player model. \$\endgroup\$ – Mike Jenkins Nov 7 '11 at 23:38
  • \$\begingroup\$ I can see that what I mean is where does that method get it's data? \$\endgroup\$ – User Nov 7 '11 at 23:40
4
\$\begingroup\$

First of all, I'd create a distinct method for every assertEquals() call. It helps defect localization. If the first assert throws an error you'll have no idea that the 2nd, 3rd and the others are OK or not. Having only one assert call per test method is a good practice. The comments should be the name of these methods. (Comment usually is a code smell.)

The $expected arrays are rather complicated. If the Player class get a new field (e.g. nickname) you probably have to modify all of your tests which could be a huge pain. I suggest using custom assert methods. For example, instead of

//Get All Available Players with No Stats Filtered by Position
$result = $this->Player->GetAvailablePlayers(1, false, null, array('def', 'mid', 'fw'));
$expected = array(...)
$this->assertEquals($expected, $result);

try

function testGetAllAvailablePlayersWithNoStatsFilteredByPosition() {
    $result = $this->Player->GetAvailablePlayers(1, false, null, 
        array('def', 'mid', 'fw'));
    $this->assertPlayerCount("playerCount", 2, $result);
    $this->assertPlayerIdEquals("player id 1", 2, $result[0]);
    $this->assertPlayerIdEquals("player id 2", 3, $result[1]);
}

(Of course you have write the assertPlayerCount and assertPlayerIdEquals methods. The first string parameter is an assertion message. It's useful if you have more than one assert call in one test method.)

In this test class you use Shared Fixture. If it's possible try using fresh fixture with Inline Setup. From Test Automation Strategy, The Three Major Fixture Strategies:

This Shared Fixture strategy is often used to improve the execution speed of tests that use a Persistent Fresh Fixture but it comes with a fair amount of baggage. These test require the use of one of the fixture construction and tear down triggering patterns. They also involve tests that interact with each other, whether by design or consequence, and this often leads to Erratic Tests (page X) and High Test Maintenance Costs.

(The cited text has links, check them.)

If you have enough time it's worth reading the whole XUnit Test Patterns book. Reading just the Part I (and Part II.) is also beneficial.

(Please note that I haven't written any PHP code in last few years, so the sample codes probably don't compile etc.)

\$\endgroup\$
  • \$\begingroup\$ Thanks, this is really useful. Will have a read through everything and hopefully tidy everything up. One other question - if you don't mind, some of my save functions update various fields in the DB. Should I test them using a custom assert function too - to ensure that the data in the DB is as expected. \$\endgroup\$ – Mike Jenkins Oct 30 '11 at 23:09
  • \$\begingroup\$ You are welcome :) Yes, you should. Just keep it DRY (Don't Repeat Yourself). Less redundancy in a the code -> easier to maintain. Custom assert methods helps to avoid redundant test code (and increase test readability etc.). \$\endgroup\$ – palacsint Oct 30 '11 at 23:46
  • 3
    \$\begingroup\$ Anyway, testing with database sounds like an integration test not an unit test. In tests try mocking the database or use in-memory databases to avoid the possibility of interacting tests. (If a test hasn't cleaned the database the next test could fail because of the remained records in the database. Life is easier with a fresh fixture.) (Furthermore, it's much faster of course.) \$\endgroup\$ – palacsint Oct 30 '11 at 23:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.