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 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题