开发者

(PHP)Unit testing with dependency injection (DI)

Over the past few days i read a lot about Dependency Injection. Now since I am trying to upgrade my phpunit skills i was thinking how to implement this DI. in my unit tests.

Say I have two objects:

class Event{
}

class Shift{
    public function __construct(Event $e)
    {
        (...)
    }
}

This how i essentially understand DI. Now I want to write a test for my shift constructor:

class ShiftTest extends
    \ModelTestCase
{
    public 开发者_Python百科function testCanCreateShift()
    {
        $e = new \Js\Entity\Event();
        $this->assertInstanceOf('JS\Entity\Shift', new \JS\Entity\Shift($e));
    }
}

But now i dont want to define here a complete event object. So what is the adviced way to create my event object in phpUnit?


This is what mocks, stubs. etc. are used for. You create a SUT (system under test), and mock out all dependencies. You wouldn't be able to do this without DI in the first place.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜