我有一个基础课,我将其注入另一个课程
AClass
{
protected $thing;
public function setThing($thing)
{
$this->thing = $thing;
}
public function getThing()
{
return $this->thing;
}
}
这个类是SUT。
AnotherClass
{
protected $aClass;
protected $someOtherClass;
__construct(AClass $aClass, SomeOtherClass $someOtherClass)
{
$this->aClass = $aClass;
$this->someOtherClass = $someOtherClass;
}
public function thatImTesting()
{
...
$thing = "logic from {$this->someOtherClass} and some other stuff";
return $this->aClass->setThing($thing);
}
}
所以我想测试AnotherClass
所以我模拟SomeOtherClass
并将其注入SUT。但是,我创建了一个新的AClass
并将其注入,因为我不想模拟这些函数(因为这没有任何意义)。
$someOtherClassMock = m::mock(SomeOtherClass::class, [
// mocking the functions here
]);
$aClass = new AClass();
$anotherClass = new AnotherClass($aClass, $someOtherClassMock);
$this->assertEquals('Something', $anotherClass->getThing());
当返回$anotherClass
对象时,我需要调用一个函数来检查测试中的数据,这仍然是一个单元测试吗?