单元测试返回注入对象

时间:2016-11-09 12:33:22

标签: php unit-testing testing

我有一个基础课,我将其注入另一个课程

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对象时,我需要调用一个函数来检查测试中的数据,这仍然是一个单元测试吗?

0 个答案:

没有答案