Question

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

No correct solution

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top