mercredi 9 novembre 2016

Unit test returning injected object

I have a basic class which I inject into another class

AClass
{
    protected $thing;

    public function setThing($thing)
    {
        $this->thing = $thing;
    }

    public function getThing() 
    {
        return $this->thing;
    }
}

This class is the 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);
    }
}

So I want to test AnotherClass so I mock SomeOtherClass and inject it into the SUT. However, I create a new AClass and inject it in because I don't want to mock the functions (as that would make no sense).

$someOtherClassMock = m::mock(SomeOtherClass::class, [
    // mocking the functions here
]);
$aClass = new AClass();
$anotherClass = new AnotherClass($aClass, $someOtherClassMock);

$this->assertEquals('Something', $anotherClass->getThing());

As $anotherClass object is returned and I need to call a function to check the data in the test, is this still a unit test?

Aucun commentaire:

Enregistrer un commentaire