15
votes

I'm trying to get Mockery to assert that a given method is called at least once.

My test class is:

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's one test that (hopefully) creates an expectation that setUri will be called. Since there isn't any other code involved, I can't imagine that it could be called and yet my test passes. Can anyone explain why?

2

2 Answers

57
votes

You need to call Mockery:close() to run verifications for your expectations. It also handles the cleanup of the mockery container for the next testcase.

public function tearDown()
{
    parent::tearDown();
    m::close();
}
2
votes

To avoid having to call the close method in every test class, you can just add the TestListener to your phpunit config like so:

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

This approach is explained in the docs.

One thing to note from the linked docs is:

Make sure Composer’s or Mockery’s autoloader is present in the bootstrap file or you will need to also define a “file” attribute pointing to the file of the above TestListener class.