Reputation: 135
I've mocked a few classes before, but this one has got me
namespace Test\Library;
class MockClassTest extends \PHPUnit_Framework_TestCase
{
public $test;
public function setUp() {
$this->test = $this->getMock('TestMockClass');
$this->test->expects($this->any())
->method('test')
->will($this->returnValue(null));
}
public function testMock() {
$result = $this->test->test();
}
}
namespace Test\Library;
class TestMockClass {
public function test() {}
}
Looks simple enough, but running this test results in: PHP Fatal error: Call to undefined method Mock_TestMockClass_ac1dda46::test() in /home/vagrant/projects/test/module/Application/test/Test/Library/MockClassTest.php
Is this a PHPUnit bug or am I missing something?
I'm using PHP 5.5.3 and PHPUnit 3.7.27.
Update (Fixed): See answer below
Upvotes: 0
Views: 1781
Reputation: 135
Update (Fixed): After looking at PHPUnit: stub methods undefined I fixed the issue. When using namespaces, getMock() needs the fully qualified namespace of the class being mocked.
I updated the code to:
namespace Test\Library;
class MockClassTest extends \PHPUnit_Framework_TestCase
{
public $test;
public function setUp() {
$this->test = $this->getMock('Test\Library\TestMockClass');
}
public function testMock() {
$this->test->expects($this->any())
->method('test')
->will($this->returnValue(null));
$this->assertEquals(null, $this->test->test());
}
}
and the test runs fine.
Upvotes: 2