doumibi6899 2016-12-14 08:53
浏览 47
已采纳

使用单元测试从标准PHP函数捕获错误消息

A bit unclear on the title, but here we go.

I am running a unit test on a method that essentially runs a strpos(). Like this:

return strpos($this->getHeaderLine($headerName), $value) !== false;

Now I also want to see what happens if I supply it an empty value. When provide an empty string, I get the following message:

strpos(): Empty needle

This message in itself is clear enough, I don't want to throw an exception here. I do want to know if it returned this message though. How would I go about doing this or do I need to rewrite my method?

  • 写回答

1条回答 默认 最新

  • dqq9695 2016-12-14 09:46
    关注

    Try the following:

    public function testWarning()
    {
        $this->expectException(PHPUnit_Framework_Error_Warning::class);
        $this->expectExceptionMessage("Empty needle");
        strpos('meh', '');
    }
    

    This test passes, but if you comment out the expectations, it fails. There are more related classes in PHPUnit framework, like PHPUnit_Framework_Error_Deprecated, PHPUnit_Framework_Error_Notice and so on.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?