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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With