dau*_*ber 1 phpunit invalidargumentexception
一切......我的主题说明了一切。我正在尝试进行一个测试,实际上故意使“为 foreach() 提供的参数无效”发生。我试图让 PHPUnit 期待它,但是......不,我的测试仍然突然停止。原因是:“为 foreach() 提供的参数无效”,就在我告诉它它将发生的确切位置。
在我的文件顶部有:
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Error\Error;
use PHPUnit\Framework\Error\Notice;
use PHPUnit\Framework\Error\Warning;
Run Code Online (Sandbox Code Playgroud)
我的班级声明开头为:
Class flirzelkwerpTest extends TestCase {
Run Code Online (Sandbox Code Playgroud)
以下是测试文件中的行:
// Let's try to add nothing.
// (It should throw an error because of "Invalid argument supplied for foreach()" in barabajagal.php.)
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid argument supplied for foreach()');
$resp = $barabajagal->add();
Run Code Online (Sandbox Code Playgroud)
错误发生在最后一行。前两行不应该告诉 PHPUnit,“嘿,小伙子,我预计这里会出现错误,这是您应该逐字得到的消息”?
我们使用 PHP 7.3.4 和 PHPUnit 8.3.2。
测试 PHP 错误时 there three things to be aware of 1) ensure that PHPUnit is converting PHP errors, warnings, and notices to exceptions, 1) ensure that PHP\'s error reporting is set appropriately, and 3) use the exceptions that PHPUnit converts the errors, warnings, and notices to.
\n\n\n\n\n默认情况下,PHPUnit 将测试执行期间触发的 PHP 错误、警告和通知转换为异常。例如,使用这些异常,您可以预期测试会触发 PHP 错误,如示例 2.12 所示。
\n\n \n
\n\n\nPHP\xe2\x80\x99s错误报告 runtime configuration can limit which errors PHPUnit will convert to exceptions. If you are having issues with this feature, be sure PHP is not configured to suppress the type of errors you\xe2\x80\x99re testing.
\n\n \n
<?php\n\nnamespace Sandbox\\Stackoverflow;\n\nuse PHPUnit\\Framework\\Error\\Warning;\n\nclass Q58222995Test extends \\PHPUnit\\Framework\\TestCase\n{\n public function testForeachWarning()\n {\n $this->expectException(Warning::class);\n $this->expectExceptionMessage(\'Invalid argument supplied for foreach()\');\n\n $collection = null;\n foreach ($collection as $value) {}\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
1417 次 |
| 最近记录: |