0

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 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?

1

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

But should i use a stub or a mock?
The biggest difference is that stubs test state (ie., after I do x, the state of y will be z), and mocks test behavior (ie., if I do x, y will happen). This is formidable article on the subject: martinfowler.com/articles/mocksArentStubs.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.