Tuesday, 15 March 2011

php - Phpunit, expecting a method exactly run two times -



php - 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, odd. testa doesnt phone call method, shouldve failed - passed, why? testb calls method once, expected twice shouldve failed - passed, why? testc ok, no question testd fails ok, no question

maybe exactly() doesnt work anticipate. im using newest 4.3.4 phpunit.

try adding method name want mock in getmock call.

for obtain aspected result modify 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 result is:

phpunit 4.3.4 sebastian bergmann. there 3 failures: 1) acme\demobundle\tests\testtest::testa expectation failed method name equal <string:method> when invoked 2 time(s). method expected called 2 times, called 0 times. 2) acme\demobundle\tests\testtest::testb expectation failed method name equal <string:method> when invoked 2 time(s). method expected called 2 times, called 1 times. 3) acme\demobundle\tests\testtest::testd testme::method() not expected called more 2 times.

hope help

php phpunit

No comments:

Post a Comment