Pregunta

Using phpspec, is it possible to run the same test with multiple values, using annotations or similar?

For example, say i have the following test:

public function it_should_return_sum_of_numbers_passed()
{
   $number1 = 1;
   $number2 = 1;

   $expectedresult = $number1 + $number2;

   $this->add($number1, $number2)->shouldReturn($expectedResult);
}

Thats fine. But it only tests a single set of parameters. What about passing -1 and 1, -1 and -2 etc etc. Fair enough this is a massively simplified scenario but it would mean having to create a new method for each edge case.

¿Fue útil?

Solución

There's no data providers in phpspec (at least not yet). You have to do something like:

public function it_should_return_sum_of_numbers_passed()
{
    $examples = array(
        array(1, 2, 3),
        array(-1, 1, 0),
        array(-1, -2, -3)
    );

    foreach ($examples as $example) {
        $number1 = $example[0];
        $number2 = $example[1];
        $expectedResult = $example[2];

        $this->add($number1, $number2)->shouldReturn($expectedResult);
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top