测试PHPUnit以测试a“__construct()必须是..”的实例;不识别异常
我有以下代码要测试类构造函数是否会触发异常,但PHPUnit测试失败。我正在努力找出我做错了什么测试PHPUnit以测试a“__construct()必须是..”的实例;不识别异常,php,phpunit,Php,Phpunit,我有以下代码要测试类构造函数是否会触发异常,但PHPUnit测试失败。我正在努力找出我做错了什么 /** @test */ public function should_require_instance_of_uuid() { $this->setExpectedException('Exception'); $id = new BusinessPartnerId; } PHPunit给出以下错误: 有1个错误: 1) Tests\Domain\Model\Common\
/** @test */
public function should_require_instance_of_uuid()
{
$this->setExpectedException('Exception');
$id = new BusinessPartnerId;
}
PHPunit给出以下错误:
有1个错误:
1) Tests\Domain\Model\Common\BusinessPartner\BusinessPartnerIdTest::是否需要\u uuid的\u实例\u
传递给Domain\Model\Common\BusinessPartner\BusinessPartnerId::uu construct()的参数1必须是Rhumsaa\Uuid\Uuid的实例,未给定,在tests/Comain/Model/Common/businesspartnerIDTest.php第14行调用并定义
Domain/Model/Common/BusinessPartner/BusinessPartnerId.php:20
tests/Domain/Model/Common/BusinessPartner/businesspartneridtest.php:14
我不知道为什么这次考试不及格?我也尝试过:
$this->setExpectedException('InvalidArgumentException')代码>您测试的外观应为:
如果你有课:
class Stack
{
public function __construct(\Model $model)
{
}
}
然后测试:
/**
* @test
*/
public function shouldCheckInstance()
{
try {
new Stack(null);
} catch(\Exception $e) {
$this->assertContains('must be an instance of Model', $e->getMessage());
}
}
您的测试应该是:
如果你有课:
class Stack
{
public function __construct(\Model $model)
{
}
}
然后测试:
/**
* @test
*/
public function shouldCheckInstance()
{
try {
new Stack(null);
} catch(\Exception $e) {
$this->assertContains('must be an instance of Model', $e->getMessage());
}
}
您的测试应该是:
如果你有课:
class Stack
{
public function __construct(\Model $model)
{
}
}
然后测试:
/**
* @test
*/
public function shouldCheckInstance()
{
try {
new Stack(null);
} catch(\Exception $e) {
$this->assertContains('must be an instance of Model', $e->getMessage());
}
}
您的测试应该是:
如果你有课:
class Stack
{
public function __construct(\Model $model)
{
}
}
然后测试:
/**
* @test
*/
public function shouldCheckInstance()
{
try {
new Stack(null);
} catch(\Exception $e) {
$this->assertContains('must be an instance of Model', $e->getMessage());
}
}
虽然我没有使用过当前的PHPUnit,但较旧的版本不允许捕获基本异常,而是可以捕获您自己对异常类的扩展。此外,异常可能是命名空间的,因此它是\exception,而不仅仅是exception
我还在doc块中使用@expectedException来表示我所期望的异常
/**
* @test
* @expectedException \MyNamespace\MyException
*/
public function shouldCheckInstance()
{
new MyClass():
}
或
虽然我没有使用过当前的PHPUnit,但较旧的版本不允许捕获基本异常,而是可以捕获您自己对异常类的扩展。此外,异常可能是命名空间的,因此它是\exception,而不仅仅是exception
我还在doc块中使用@expectedException来表示我所期望的异常
/**
* @test
* @expectedException \MyNamespace\MyException
*/
public function shouldCheckInstance()
{
new MyClass():
}
或
虽然我没有使用过当前的PHPUnit,但较旧的版本不允许捕获基本异常,而是可以捕获您自己对异常类的扩展。此外,异常可能是命名空间的,因此它是\exception,而不仅仅是exception
我还在doc块中使用@expectedException来表示我所期望的异常
/**
* @test
* @expectedException \MyNamespace\MyException
*/
public function shouldCheckInstance()
{
new MyClass():
}
或
虽然我没有使用过当前的PHPUnit,但较旧的版本不允许捕获基本异常,而是可以捕获您自己对异常类的扩展。此外,异常可能是命名空间的,因此它是\exception,而不仅仅是exception
我还在doc块中使用@expectedException来表示我所期望的异常
/**
* @test
* @expectedException \MyNamespace\MyException
*/
public function shouldCheckInstance()
{
new MyClass():
}
或