0

My code:

public function testApiClient()
{
    $mock = createMockApiClient();
    dump($mock->someMethod());
}

protected function createMockApiClient()
{

    $mockApiClient = $this->createMock(ApiClient::class);
    return $mockApiClient
        ->expects($this->any())
        ->method('someMethod')
        ->will($this->returnValue('someString'))
    ;
}

So I have this error:

Error: Call to undefined method 
PHPUnit_Framework_MockObject_Builder_InvocationMocker::someMethod()

I'm new in PHPUnit tests, so what to do? I can't understand why I do as wrote in example and no result - just error?

1 Answer 1

2

So I started searching in google. Strange: no one answer. Searching on PHPUnit Github repository didn't get any solution or even tips how to solve problem.

After many different tries I !found! that method will return PHPUnit_Framework_MockObject_Builder_InvocationMocker class, and if I just write after will semicolon and then return $mockApiClient it returns Mock_ApiClient_anyHash!

So you have to finish build your mock and only then return it.

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

Comments

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.