首页 > 解决方案 > 尝试编写 PHP 单元测试时不推荐使用方法“setMethods”

问题描述

我想创建一个模拟来替换资源:

$gateway = $this->getMockBuilder('PaymentGateway')
            ->setMethods(['transfer'])
            ->getMock();

我收到了这个警告:

方法“setMethods”已弃用

如何解决此弃用?

标签: phptestingphpunit

解决方案


从现在开始,我们应该使用任何一个onlyMethods()(这将最接近setMethods()and addMethods()

$gateway = $this->getMockBuilder('PaymentGateway')
            ->onlyMethods(['transfer'])
            ->getMock();

这在PR中进行了解释(直接从方法 PHP 文档链接,如此处所示)。


推荐阅读