Home >Backend Development >PHP Tutorial >PHPUnit如何mock函数内部实例化的对象

PHPUnit如何mock函数内部实例化的对象

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-23 13:54:511426browse

class cash{    public function __construct(){}    public function isCashed(){       //do something        return 1;    }}class sendMsg{    public function send(){          $cash = new cash();          $isCashed = $cash->isCashed();          return $isCashed;         //do something    }}

我想要对上面的sendMsg进行测试的时候 请问怎样mock出cash类然后注入到sendMsg当中

class sendMsgTest extension PHPUnit_Framework_TestCase{             public function testSend(){             $sendMsg = new sendMsg();                          //如何mock一个cash      }}


回复讨论(解决方案)

class cash 这个已经定义好了吗,还是没有定义?

class cash 这个已经定义好了吗,还是没有定义?

 都已经定义好了,是对现有代码写测试,上面那个只是我简化了的个东西

可以试试phpunit phpmock

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn