Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phpunit, expecting a method exactly run two times

class TestMe
{
    public function method() { }
}

test:

class TestTest extends PHPUnit_Framework_TestCase
{
    public function testA()
    {
        $stub = $this->getMock ('TestMe');
        $stub->expects ($this->exactly(2))->method('method');
    }

    public function testB()
    {
        $stub = $this->getMock ('TestMe');
        $stub->expects ($this->exactly(2))->method('method');
        $stub->method();
    }

    public function testC()
    {
        $stub = $this->getMock ('TestMe');
        $stub->expects ($this->exactly(2))->method('method');
        $stub->method();
        $stub->method();
    }

    public function testD()
    {
        $stub = $this->getMock ('TestMe');
        $stub->expects ($this->exactly(2))->method('method');
        $stub->method();
        $stub->method();
        $stub->method();
    }
}

testA, testB, testC passes, testD fails only, this is odd. testA doesnt even call the method, so it shouldve failed - but it passed, why? testB calls the method ONCE, but we expected TWICE so it shouldve failed - but it passed, why? testC is OK, no question testD fails so its OK, no question

maybe the exactly() doesnt work exactly I anticipate. Im using the newest 4.3.4 PhPunit.

like image 268
John Smith Avatar asked Mar 19 '23 00:03

John Smith


1 Answers

Try adding the method name you want to mock in the getMock call.

For obtain the aspected result i modify the Test Class as:

class TestTest extends \PHPUnit_Framework_TestCase
{
    public function testA()
    {
        $stub = $this->getMock ('TestMe',array('method'));
        $stub->expects ($this->exactly(2))->method('method');
    }

    public function testB()
    {
        $stub = $this->getMock ('TestMe',array('method'));
        $stub->expects ($this->exactly(2))->method('method')->withAnyParameters();
        $stub->method();
    }

    public function testC()
    {
        $stub = $this->getMock ('TestMe',array('method'));
        $stub->expects ($this->exactly(2))->method('method')->withAnyParameters();
        $stub->method();
        $stub->method();
    }

    public function testD()
    {
        $stub = $this->getMock ('TestMe',array('method'));
        $stub->expects ($this->exactly(2))->method('method')->withAnyParameters();
        $stub->method();
        $stub->method();
        $stub->method();
    }
}

And the result is:

PHPUnit 4.3.4 by Sebastian Bergmann.

There were 3 failures:

1) Acme\DemoBundle\Tests\TestTest::testA
Expectation failed for method name is equal to <string:method> when invoked 2 time(s).
Method was expected to be called 2 times, actually called 0 times.

2) Acme\DemoBundle\Tests\TestTest::testB
Expectation failed for method name is equal to <string:method> when invoked 2 time(s).
Method was expected to be called 2 times, actually called 1 times.

3) Acme\DemoBundle\Tests\TestTest::testD
TestMe::method() was not expected to be called more than 2 times.

Hope this help

like image 123
Matteo Avatar answered Mar 26 '23 02:03

Matteo