简体   繁体   中英

phpunit - Testing constructor with dependency injection

I need to test following constructor which has dependency injection using phpunit. For that do I have to use mocking ? If so please give an example.

/**
 * Set graph object
 *
 * @param Graph $graphObj graph object
 */
public function __construct(Graph $graphObj)
{
    $this->graphObject = $graphObj;
}

Assuming phpunit:

$mockGraph = $this->getMockBuilder('Graph')
                  ->setMethods(array('update'))
                  ->getMock();
$testObject = new Object($mockGraph);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM