对象创建中的 PHPUnit 测试断言
PHPUnit test assert in object create
当我 运行 phpunit 我得到:
1) FooTests::testException assert(): Assertion "false" failed
我希望在我有的情况下得到断言。
class FooTests extends WP_UnitTestCase {
protected $foo;
public function setUp() {
parent::setUp();
$this->foo = new Foo();
}
function testException() {
// I'd like to expect an assert in the class foo so the test should not fail.
$this->foo->test();
}
}
class Foo {
public function __construct(){
}
public function __destruct(){}
public function test(){
assert('false');
}
}
您可以通过以下方式之一实现:
1) 捕获PHP单元警告异常
PHP 为每个失败的断言发出警告,因此 PHPUnit 引发异常
PHPUnit_Framework_Error_Warning
类型。如 doc:
中所述
By default, PHPUnit converts PHP errors, warnings, and notices that
are triggered during the execution of a test to an exception.
[..]
PHPUnit_Framework_Error_Notice
and PHPUnit_Framework_Error_Warning
represent PHP notices and warnings, respectively.
所以你可以简单地通过以下方式捕获:
public function testException() {
$this->expectException(\PHPUnit_Framework_Error_Warning::class);
$this->foo->test();
}
2) 在断言失败时使用回调
您可以使用 assert_options 做一些更清楚的事情,使用自定义异常作为回调并将其作为示例处理:
public function test_using_assert_options_PHP5()
{
$fnc = function() {
throw new \Exception('assertion failed', 500);
};
$this->expectException(\Exception::class);
$this->expectExceptionCode(500);
$this->expectExceptionMessage('assertion failed');
assert_options(ASSERT_CALLBACK, $fnc);
$this->foo->test();
}
3) 更改失败异常的行为(仅来自 PHP7)
如果您正在使用 PHP7,您可以使用名为 assert.exception:
的新设置来实现最后一个行为
public function test_using_assert_options_PHP7()
{
$this->expectException(\AssertionError::class);
assert_options(ASSERT_EXCEPTION, 1);
$this->foo->test();
}
希望对您有所帮助
当我 运行 phpunit 我得到:
1) FooTests::testException assert(): Assertion "false" failed
我希望在我有的情况下得到断言。
class FooTests extends WP_UnitTestCase {
protected $foo;
public function setUp() {
parent::setUp();
$this->foo = new Foo();
}
function testException() {
// I'd like to expect an assert in the class foo so the test should not fail.
$this->foo->test();
}
}
class Foo {
public function __construct(){
}
public function __destruct(){}
public function test(){
assert('false');
}
}
您可以通过以下方式之一实现:
1) 捕获PHP单元警告异常
PHP 为每个失败的断言发出警告,因此 PHPUnit 引发异常
PHPUnit_Framework_Error_Warning
类型。如 doc:
By default, PHPUnit converts PHP errors, warnings, and notices that are triggered during the execution of a test to an exception.
[..]
PHPUnit_Framework_Error_Notice
andPHPUnit_Framework_Error_Warning
represent PHP notices and warnings, respectively.
所以你可以简单地通过以下方式捕获:
public function testException() {
$this->expectException(\PHPUnit_Framework_Error_Warning::class);
$this->foo->test();
}
2) 在断言失败时使用回调
您可以使用 assert_options 做一些更清楚的事情,使用自定义异常作为回调并将其作为示例处理:
public function test_using_assert_options_PHP5()
{
$fnc = function() {
throw new \Exception('assertion failed', 500);
};
$this->expectException(\Exception::class);
$this->expectExceptionCode(500);
$this->expectExceptionMessage('assertion failed');
assert_options(ASSERT_CALLBACK, $fnc);
$this->foo->test();
}
3) 更改失败异常的行为(仅来自 PHP7)
如果您正在使用 PHP7,您可以使用名为 assert.exception:
的新设置来实现最后一个行为public function test_using_assert_options_PHP7()
{
$this->expectException(\AssertionError::class);
assert_options(ASSERT_EXCEPTION, 1);
$this->foo->test();
}
希望对您有所帮助