John Smith
John Smith

Reputation: 6207

Phpunit mock has no effect

code:

class MockMe
{
    public function mockMeee()
    {
        return 'Im not mocked';
    }
}

test:

$sut = new MockMe();
$this
    ->getMock(get_class($sut))
    ->expects($this->any())
    ->method('mockMeee')
   ->will($this->returnValue('Im finally mocked'));
echo $sut->mockMeee();

this outputs the original "Im not mocked", but it supposed to send the Im finally mocked text. What is wrong?

EDIT: done:

$stub = $this->getMock('MockMe');
$stub->method('mockMeee')->willReturn('Im finally mocked');
echo $stub->mockMeee();

Upvotes: 0

Views: 260

Answers (1)

Drumbeg
Drumbeg

Reputation: 1944

You are constructing the real MockMe, then building a mock that you are doing nothing with. I think your test should be something like:

$sut = $this->getMock('MockMe');

$sut->expects($this->any())
    ->method('mockMeee')
    ->will($this->returnValue('Im finally mocked'));

echo $sut->mockMeee();

Refer to http://phpunit.de/manual/4.2/en/test-doubles.html for more information on PHPUnit mocks.

Upvotes: 2

Related Questions