Warm tip: This article is reproduced from serverfault.com, please click

testing-尝试编写PHP单元测试时,不建议使用方法'setMethods'

(testing - Method 'setMethods' is deprecated when try to write a PHP Unit Test)

发布于 2020-11-30 14:14:13

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

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

我收到此警告:

方法'setMethods'已过时

我该如何解决此弃用问题?

Questioner
Khaled Boussoffara
Viewed
0
yivi 2020-11-30 22:26:29

从现在开始,我们应该使用其中任何一种onlyMethods()(这与setMethods()最接近addMethods()

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

PR中对此进行了解释(直接从方法PHP doc链接,如此处所示)。