I am testing an application with phpunit, and I have found a very rare case. I have an object with a method similar to the following:
public function validar(array $array = [])
{
if(empty($array['clave'])
throw new \Exception("no hay clave");
// otras validaciones...
//fin
}
This method checks the array passed to it as a parameter, and if there is an error, it throws the corresponding exception. If correct, the flow continues its course.
When trying to do the test in the event that all the parameters are correct, I do the following:
public function testValidarHappyCase()
{
$objeto = new MiClase();
$params = [
'clave' => 1,
'user' => 3,
'publicacion' => 3
];
$objeto->validar();
}
The problem is that when I run the phpunit, I get this:
Testing App\Tests\Clases\MiClaseTest
.R.. 4 / 4 (100%)
Time: 21.68 seconds, Memory: 42.50 MB
There was 1 risky test:
1) App\Tests\Clases\MiClasetest::testValidarHappyCase
This test did not perform any assertions
/zen/tests/Clases/MiClaseTest.php:41
OK, but incomplete, skipped, or risky tests!
Tests: 4, Assertions: 5, Risky: 1.
As the method doesn't return any value, I can't do any assert with the result, to validate if it is correct or not. I really don't know how to test this.
It occurred to me that I could add a return 1 or something like that, so that a result would be validated in the test. The problem is that by specification of the app I can't touch those objects...
Does anyone have any idea to check this?
For this case you can make use of the annotation:
For example: