Mockery shouldReceive () & # 8594; once () doesn't seem to work

I am trying to force Mockery to claim that this method is called at least once.

My test class:

use \Mockery as m; class MyTest extends \PHPUnit_Framework_TestCase { public function testSetUriIsCalled() { $uri = 'http://localhost'; $httpClient = m::mock('Zend\Http\Client'); $httpClient->shouldReceive('setUri')->with($uri)->atLeast()->once(); } } 

As you can see, there is one test that (hopefully) creates an expectation of a call to setUri. Since there is no other code involved, I cannot imagine what it could be called, and yet my test passes. Can someone explain why?

+6
source share
2 answers

You need to call Mockery:close() to perform checks for your expectations. It also handles cleaning the bullying container for the next test test.

 public function tearDown() { parent::tearDown(); m::close(); } 
+38
source

To avoid calling the close method in each test class, you can simply add TestListener to your phpunit configuration as follows:

 <listeners> <listener class="\Mockery\Adapter\Phpunit\TestListener"></listener> </listeners> 

This approach is explained in docs .

There is one thing in related documents:

Make sure that the Composers or Mockerys autoloader is present in the bootstrap file, or you will also need to define the β€œfile” attribute that points to the file of the above TestListener class.

+2
source