php - PHPUnit 9 支持将 expectException() 与 PHPUnit\Framework\Error\Error 一起使用已弃用
问题描述
我正在将 phpunit 从 7.5 更新到 9.5,我遇到了一条警告消息:
Support for using expectException() with PHPUnit\Framework\Error\Error is deprecated and will be removed in PHPUnit 10. Use expectError() instead.
使用以下示例测试:
public function testFooErrorsOnBlankBar()
{
$this->expectException('InvalidArgumentException');
My\Class::foo('', '123');
}
Wherefoo
有一个必需的参数bar
。
我可以将此测试更新为,expectErrorMessage
但随后我断言消息(类似于expected bar to be set
)而不是错误类型,在我看来,作为开发人员更容易阅读。
我可能在phpunit 的文档中遗漏了一些简单的东西,但我一般是 php 文档的新手。任何帮助,将不胜感激!
解决方案
https://thephp.cc/news/2020/02/migrating-to-phpunit-9
根据我expectException()
的发现,应该可以捕获所有不同类型的错误,即弃用、通知、警告和错误。因此维护人员决定为每种类型使用单独的方法,即expectDeprecation()
,和expectNotice()
expectWarning()
expectError()
在我看来,维护人员一开始并不打算expectException()
指定错误的类型,他们只想知道它是否已被弃用、通知、警告或错误,我部分同意。在大多数情况下无法捕获足够的错误,除非您的代码可以抛出不同类型的错误并且您希望确保它抛出正确类型的错误。
对于您的代码expectError()
应该足够了。
public function testFooErrorsOnBlankBar()
{
$this->expectError();
My\Class::foo('', '123');
}
推荐阅读
- angular - 为什么即使我添加了 takeUntil 和 skipWhile,ngrx 也会返回 2 个对象?
- javascript - 你能取消初始化一个javascript函数吗?
- c# - 是否可以将 WPF 数据网格上的用户输入数据存储到字符串 var 中?
- python - 在 Python 中的 If 语句中删除对象
- node.js - Akka 演员与 nodejs 工作线程。为什么(如果)演员更好?
- c++ - 如何在 Windows 上的 C++ 中通过蓝牙实现串行通信?
- java - jooq 嵌套选择和类型安全
- c# - 重写以下方法的更好方法
- powershell - 非域帐户或本地用户帐户的凭据检查
- git - 如何在git中推送到正确的分支