V
V
vrazbros2021-06-11 20:12:59
PHP
vrazbros, 2021-06-11 20:12:59

What tests can be written for the functionality of creating an object and calling methods?

There is this conditional code in php:

$player1 = new Player('Vasa', new SimpleChoise());
$player2 = new Player('Peta', new RandomChoise());

$game = new Game(new StoneScissorPaperGameStrategy(), $player1, $player2);

$result = [];

for ($i = 0; $i < 10; $i++) {
    $winnerName = $game->run();
    if(isset($result[$winnerName])) {
        $result[$winnerName]++;
    } else {
        $result[$winnerName] = 1;
    }
}

foreach ($result as $player => $value)
{
    echo sprintf("\nPlayer %s wins %s times", $player, $value);
}


how many and what kind of tests would you write for such code? and is it well written if you can rate it?
I could only come up with a code to check the return type for $game->run(), but how to check in tests that an object of the type player $player1 = new Player was created? how to check in tests whether data is processed correctly in cycles?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question