问题
Given the following code:
<?php
class Language
{
private $code;
public function __construct(string $code)
{
$this->code = $code;
}
}
$lang = new Language();
I will get an ArgumentCountError like this:
Uncaught ArgumentCountError: Too few arguments
Now I would like to test and ensure this beahviour with a phpUnit unit test like this one:
class LanguageTest
{
public function testLanguageConstructorWillRequireACode()
{
$language = new Language();
$this->expectException(MissingArgumentException::class);
}
}
But php interpeter raise an error and not an exception.
What would be the most idiomatical and correct way to do this?
来源:https://stackoverflow.com/questions/48486568/catch-an-error-as-exception