Error when mocking interfaces in PHP using Mockery
Asked Answered
R

2

9

I have run into a problem when mocking Interfaces using Mockery in PHP (im using the laravel framework but I'm not sure this is relevant.

I have defined an interface

<?php namespace MyNamespace\SomeSubFolder;

interface MyInterface {

    public function method1();

}

And I have a class that typehints that interface on one of the methods...

<?php namespace MyNamespace\SomeSubFolder;

use MyNamespace\SomeSubFolder\MyInterface;

class MyClass {

    public function execute(MyInterface $interface)
    {
         //does some stuff here

    }
}

...and I am trying to test MyClass. I have created a test that looks something like this:

public function testExecute()
{

    $mock = Mockery::mock('MyNamespace\SomeSubFolder\MyInterface');

    $mock->shouldReceive('method1')
         ->andReturn('foo');

    $myClass = new MyClass();

    $myClass->execute($mock);

}

When I run the test I receive the message

'ErrorException: Argument 1 passed to MyClass::execute must be an instance of MyNamespace\SomeSubFolder\MyInterface, instance of Mockery_123465456 given....'

I have no idea why.

Within the test I have tried the following :

$this->assertTrue(interface_exists('MyNamespace\SomeSubFolder\MyInterface'));

$this->assertTrue($mock instanceof MyInterface);

and both return true, so it appears as if I have created and instance that implements the interface, but when I call the method on the class it disagrees. Any ideas???

Rowenarowland answered 20/10, 2013 at 15:15 Comment(3)
I think you'll need to provide more code or your actual code, this gist.github.com/davedevelopment/31b143fc44ec52fcc575 works as expected for meDjambi
I get this as well when using Mockery. I have found that running tests with process isolation enabled works (but this is noticeably slower).Predial
I do remember having a similar problem which actually turned out to be a bug in Mockery. Can you update Mockery to dev-master and see if the problem still persists?Superior
R
17

You should call mock() method at the and of mock declaration.

$mock = Mockery::mock('MyNamespace\SomeSubFolder\MyInterface');

$mock->shouldReceive('method1')
     ->andReturn('foo')
     ->mock();
Returnable answered 17/9, 2016 at 9:38 Comment(2)
I'm also interested where this comes from. It's not mentioned anywhere in documentation. But my mocks basically always require this extra ->mock() call.Corwun
Figured it out: I was wrongly chaining methods like $mock = Mockery::mock(...)->shouldReceive(); which should actually be $mock = ...; $mock->shouldReceive();Corwun
R
-1

I think the problem is with PHP class loading. Your 'MyNamespace\SomeSubFolder\MyInterface' class isn't available from your test file.

You'll need to modify your composer.json to autoload that namespace or you'll need a require_once('path/to/file/containing/namespace/MyInterface.php') at the top of your test.

Although, when you did try the interface_exists it seemed to pass. It could also be that you misspelled your namespaced class when you mocked it. I've made that mistake as well.

In any case, Mockery isn't able to see that the class exists so it's just inventing one.

Can you provide your full test source?

Rubin answered 4/3, 2014 at 22:41 Comment(1)
He's not using a constructor.Edwin

© 2022 - 2024 — McMap. All rights reserved.