user3419148
user3419148

Reputation: 117

Mocking interface with mockery?

I am trying to create a mockery-mock for some interfaces in my tests.

But the result of the mocking is a mock-object that is not an instance of the interface. E.g. the generated mock-class does not ' implements MyInterface '.

I properly checked and found that Mockery CAN&DOES locate and load my interface (by adding some echo's in the mockery classes)

Is it really true that mockery does not declare that a mocked-class implements it requested interface? Doesn't this completely bypass the good practices of TTD?

$mock = m::mock('My\\Cool\\Interface');
//now $mock is not aninstanceof My\Cool\Interface

Hope to hear from you.

Upvotes: 3

Views: 5672

Answers (2)

honzalilak
honzalilak

Reputation: 376

For a while I thought that mocking interfaces doesn't work for me as well but eventually I found a typo which caused the mock not to work.

At the moment mocking interfaces works for me.

Here is an example.

Example Interface

interface Convertor
{

    /**
     * @return array
     */
    public function getIds();

}

Creating the mock in phpunit test

    $convertor = Mockery::mock('Convertor');
    $convertor->shouldReceive('getIds')->andReturn([10, 20]);

Can you post the code which doesn't work for you?

Upvotes: 3

user3419148
user3419148

Reputation: 117

To answer my own question: yes, mockery does no proper job in implementing a mock-object for an interface.

To me, this kind of makes mockery useless for proper test-driven-development. :-(

Upvotes: 1

Related Questions