Lunfel
Lunfel

Reputation: 2677

PHPUnit error call to undefined method on mock

I am getting Error: Call to undefined method Mock_SimpleInterface_8a93e777::mymethod() when I call the mymethod() on the Simple class mock.

class PlaygroundTest extends \PHPUnit_Framework_TestCase 
{
    public function testMock()
    {
        $class     = $this->getMockBuilder('\Playground\Simple')->getMock();

        $class->mymethod();
    }
}

The Simple class implementation

namespace Playground;

class Simple
{

    public function mymethod()
    {
        print "Hey!";
    }
}

According to PHPUnit docs (https://phpunit.de/manual/5.1/en/test-doubles.html), it states that "By default, all methods of the original class are replaced with a dummy implementation that just returns null (without calling the original method)."

Shouldn't I be able to call mymethod() and get a null return value? I want to avoid to specify all class methods. PHPUnit should be clever enough to know which methods can be called on the mock or not.

Is this a bug? I'm using PHPUnit 5.1.4

Upvotes: 2

Views: 4959

Answers (1)

Fabian Schmengler
Fabian Schmengler

Reputation: 24551

Your assumptions are correct, so you have an error somewhere else or did not show the real code.

The mock class name Mock_SimpleInterface_8a93e777 suggests that you don't actually mock \Playground\Simple but rather \Playground\SimpleInterface, which probably does not contain mymethod()

Upvotes: 2

Related Questions