doushang4293 2009-12-13 15:05
浏览 66
已采纳

PHPUnit异常测试,错误消息混淆结果输出

I can't seem to correctly do this, the error message of the exception just prints out, making the command line window harder to read. Below is how my code is structured and the 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');
}

The error message will print out in the command line window like below. I tried all the methods shown in phpunit.de but same results.

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

The error line prints out, how do I suppress that, am I doing it right at all?

  • 写回答

2条回答 默认 最新

  • douluoxiao2286 2009-12-13 15:27
    关注

    This is embarrassing as I found just the way to do it. Thanks Chris, but I tried that as well.

    I tested the wrong method, chooseFruit isn't the method that throws the exception, so the exception error prints out:

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

    Testing the actual method that throws the exception will mute the error message, since it is not echoed at all:

    public function testAvailableFruits()
    {
      $this->setExpectedException('Exception');
      **availableFruits('Papaya')**
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀