加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

phpunit测试expectException无法正常工作

发布时间:2020-05-25 08:46:36 所属栏目:PHP 来源:互联网
导读:我正在尝试测试我的类InvalidArgumentException,但我得到了 TestsBarTest::should_receive_parameter Missing argument 1 for ItdcFooBar::__construct(), called in /mypath/foo/tests/BarTest.php on line 10 and defined 这是我使

我正在尝试测试我的类InvalidArgumentException,但我得到了

TestsBarTest::should_receive_parameter Missing argument 1 for
ItdcFooBar::__construct(),called in
/mypath/foo/tests/BarTest.php on line 10 and
defined

这是我使用的测试(BarTest.php)文件:

<?php namespace Tests;
use ItdcFooBar;
class BarTest extends PHPUnit_Framework_TestCase {
    /** @test */
    public function should_receive_parameter() {
        $this->setExpectedException('Exception');
        $id = new Bar;
    }
}

这是Bar类:

<?php namespace ItdcFoo;
class Bar {
    public function __construct($a) {
        // do something
    }
}

我试图在注释部分puth setExpectedException,也试图使用InvalidArgumentException,但没有运气.

任何建议我做错了将不胜感激.

如果你直接捕获它,你可以var_dump()异常.
/** @test */
public function shouldThrowException() {
  try {
    new Foo();
  } catch (Exception $e) {
    var_dump($e);
  }
}

输出:

class PHPUnit_Framework_Error_Warning#19 (9) {
  ...

所以这里的源不会触发异常而是错误,PHPUnit会将错误转换为特定的异常.但它不包含在断言中.

尝试使用特定的例外名称:

/** @test */
public function shouldThrowException() {
  $this->setExpectedException('PHPUnit_Framework_Error_Warning');
  new Foo();
}

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读