Jayy
Jayy

Reputation: 14728

PHPUnit fatal error call to undefined method on mock

I want to test that a method on an object is called. I want to do this by making a mock of Object, rather than any specific class. But the following code throws a fatal error:

class MyTest extends PHPUnit_Framework_TestCase
{
    public function testSomeMethodIsCalled()
    {
        $mock = $this->getMock('Object');
        $mock->expects($this->once())
                ->method('someMethod');
        $mock->someMethod();
    }
}

The above dies with an error:

Fatal error: Call to undefined method Mock_Object_204ac105::someMethod()

I'm sure there was a way to do this, without having to write a class that actually has a someMethod() method?

Upvotes: 1

Views: 2761

Answers (1)

Nikita U.
Nikita U.

Reputation: 3618

You must set array of methods that should be available in mock when you create it via $this->getMock(), so this code should work:

$mock = $this->getMock('Object', ['someMethod']);

Upvotes: 1

Related Questions