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

一世执手 提交于 2019-12-18 19:05:16

问题


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?


回答1:


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:


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.



来源:https://stackoverflow.com/questions/16035549/mockery-shouldreceive-once-doesnt-seem-to-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!