Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phpunit mock - method does not exist

I recently updated PHPunit from 5.3 to 5.5 in an IntegrationTestCase of an app that is CakePhp 3.x based. and I don't understand how to update my mock generation scripts.

Originally I created my mock like this:

$stub = $this->getMock('SomeClass', array('execute'));
$stub->method('execute')
     ->will($this->returnValue($this->returnUrl));

After the change to PHPUnit 5.5 this got me the following warning:

PHPUnit_Framework_TestCase::getMock() is deprecated,
use PHPUnit_Framework_TestCase::createMock()
or PHPUnit_Framework_TestCase::getMockBuilder() instead

In order to fix this warning I changed the mock-generation to:

$stub = $this->getMockBuilder('SomeClass', array('execute'))->getMock();
$stub->method('execute')
     ->will($this->returnValue($this->returnUrl));```

Now I get the following error message when running the test:

exception 'PHPUnit_Framework_MockObject_RuntimeException' 
with message 'Trying to configure method "execute" which cannot be
configured because it does not exist, has not been specified, 
is final, or is static'

Anybody know, how to avoid this error? Thank you.

like image 484
David Albrecht Avatar asked Sep 20 '16 18:09

David Albrecht


People also ask

What are mocks in PHPUnit?

The essence of a mock is that instead of a dependency object, you use a special object in which all the methods of the original class have been replaced. For such an object, you can configure the results returned by the methods and add checks for method calls. PHPUnit has a built-in mechanism for working with mocks.

What happened to createpartialmock() in PHPUnit?

createPartialMock () called with method (s) timerStopt that do not exist in PhpUnitMockDemo\AbstractCommand. This will not be allowed in future versions of PHPUnit. Unfortunately, this change was not reflected in the documentation — only setMethods () was still described there, and everything else was hidden in the depths of code and GitHub.

What is a mock object in testing?

The practice of replacing an object with a test double that verifies expectations, for instance asserting that a method has been called, is referred to as mocking. You can use a mock object “as an observation point that is used to verify the indirect outputs of the SUT as it is exercised.

What is mocking in Java?

The practice of replacing an object with a test double that verifies expectations, for instance asserting that a method has been called, is referred to as mocking. Lets assume we have SomeService to test.


2 Answers

PHPUnit_Framework_TestCase::getMockBuilder() only takes one (1) argument, the class name. The methods to mock are ment to be defined via the returned mock builder objects setMethods() method.

$stub = $this
    ->getMockBuilder('SomeClass')
    ->setMethods(['execute'])
    ->getMock();

See also

  • PHPUnit Manual > Test Doubles > Mock Objects
like image 95
ndm Avatar answered Sep 27 '22 22:09

ndm


I will leave this as an answer to myself, when i reach this problem again:

The mocked method may not be private.

like image 21
scones Avatar answered Sep 27 '22 21:09

scones