首页 > 解决方案 > PHPUnit 中的部分模拟现在 setMethods( 已弃用

问题描述

我正在尝试使用 PHP Unit 实现类的部分模拟,其中仅当显式设置存根行为时,存根方法才会被存根,如果没有保留类方法的原始行为;

我知道使用 setMethods( 允许我像在 => 中那样做

/**
 * at this point, if I stub the class like this, only the constructor is called 
 * and all of the stubbed class methods will return null if I don't set them later in my test ...
 */
// $stubbed = $this->getMockBuilder(Stubbed::class)->getMock();
/**
 *  but... there is a way to keep the original methods
 *  to be able to stub them only in an explicit manner;
 *  the problem is, setMethods( seems to be deprecated
 */
$stubbed = $this->getMockBuilder(Stubbed::class)->setMethods(["doSomethingToBeStubbed"])->getMock();
// ... creating some stubbed methods
$stubbed->method("doSomethingToBeStubbed")->willReturn(true);

所以基本上我想知道是否有更好的方法来写这个以避免将来的技术债务(因为弃用通知)

标签: unit-testingphpunit

解决方案


推荐阅读