【发布时间】:2016-09-06 09:31:51
【问题描述】:
假设在我的类的构造函数中我注入了另一个类。在我的一种方法中,我的回报取决于那个班级。如何测试该回报?
class MyClass {
protected $directoryThatIWant;
public function __construct(
\AnotherClass $directoryThatIWant
)
{
$this->directoryThatIWant = $directoryThatIWant;
}
public function getVarFolderPath()
{
return $this->directoryThatIWant->getPath('var');
}
}
还有我的测试课:
class MyClassTest extends \PHPUnit_Framework_TestCase
{
const PATH = 'my/path/that/I/want';
protected $myClass;
protected function setUp()
{
$directoryThatIWantMock = $this->getMockBuilder(AnotherClass::class)
->disableOriginalConstructor()
->getMock();
$this->myClass = new myClass(
$directoryThatIWantMock
);
}
public function testGetVarFolderPath()
{
$this->assertEquals(self::PATH, $this->myClass->getVarFolderPath());
}
}
来自模拟对象的方法返回 null,所以我不确定如何确保 $this->myClass->getVarFolderPath() 返回我想要的值。
谢谢!
【问题讨论】:
标签: php unit-testing phpunit