Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockery shouldReceive()->once() doesn't seem to work

Tags:

php

mockery

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?

like image 382
Jez Avatar asked Apr 16 '13 11:04

Jez


2 Answers

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();
}
like image 86
Bram Gerritsen Avatar answered Nov 11 '22 08:11

Bram Gerritsen


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.

like image 20
Hassan Avatar answered Nov 11 '22 06:11

Hassan