Phpunit test exception, error message puts result

I can’t do it right, the exception error message just prints out, which makes it difficult to read the command prompt window. The following is the procedure for structuring my code and test code.

public function availableFruits($fruit)
{
  switch($fruit) {
    case 'foo':
    // all good
    break;

    case 'bar':
    // all good
    break;

    default:
    throw new Exception($fruit.' not available!');
    break;

  }
}

public function chooseFruit($fruit)
{
  try {
    availableFruits($fruit);
  } catch (Exception $e) {
    echo $e->getMessage();
  }
}

public function testAvailableFruits()
{
  $this->setExpectedException('Exception');

  chooseFruit('Kiwi');
}

An error message will be printed on the command line window, as shown below. I tried all the methods shown in phpunit.de, but the same results.

..Error on line 13 in c:\file\path\fruits.php: Kiwi not available!.F

The error line prints, how can I suppress it, am I doing it right?

+3
source share
2 answers

This is confusing when I found a way to do this. Thanks to Chris, but I tried it too.

, - , , :

public function testAvailableFruits()
{
  $this->setExpectedException('Exception');
  **chooseFruit('Kiwi');**
}

, , , :

public function testAvailableFruits()
{
  $this->setExpectedException('Exception');
  **availableFruits('Papaya')**
}
+1

, - , ...

/**
 * @expectedException ExpectedExceptionName
 */

, , , catch.

+3

Source: https://habr.com/ru/post/1725540/


All Articles