<?php
class Target
{
public function validate()
{
$this->getData();
return true;
}
public function getData()
{
return array();
}
}
<?php
class TargetTest extends PHPUnit_Framework_TestCase
{
public function testValidate()
{
$mock = m::mock('Target');
$mock->shouldReceive('getData')
->once();
$expected = $this->exp->validate();
$this->assertTrue($expected);
}
}
Mockery\Exception\InvalidCountException: Method getData() from Mockery_1_ExpWarning should be called
exactly 1 times but called 0 times.
我使用Mockery
作为模拟工具,示例总是关于如何使用DI
进行模拟,我想知道我可以模拟内部方法吗?
答案 0 :(得分:3)
您可以使用测试框架的Partial Mocks features仅模拟getData
方法并描述期望。
As(工作)示例:
use Mockery as m;
class TargetTest extends \PHPUnit_Framework_TestCase
{
public function testValidate()
{
$mock = m::mock('Target[getData]');
$mock->shouldReceive('getData')
->once();
$expected = $mock->validate();
$this->assertTrue($expected);
}
}
希望这个帮助