drgwsx8405 2014-11-10 10:30
浏览 23
已采纳

too long

We are experimenting a strange behaviour of Mockery (0.9.2) while tdd-ing a Symfony controller which makes use of several request parameters grabbed using the Request service. We are using PHPUnit (3.7) as testing framework.

The way we approach TDD is using setUp method for creating mocks and configuring them by using byDefault() so they can provide a neutral happy-flow scenario. Then, in each test method, we become specific about our expectations about mock behaviour.

I isolated the problem in a proof of concept test just for making the analysis easier. Here we go.

This is the test class itself:

class FooTest extends \PHPUnit_Framework_TestCase
{
    private $request;

    public function setUp()
    {
        $this->request = \Mockery::mock('Symfony\Component\HttpFoundation\Request');

        $this->request->shouldReceive('get')->with('a')->andReturnNull()->byDefault();
        $this->request->shouldReceive('get')->with('b')->andReturnNull()->byDefault();
    }

    public function test_bar_checks_request_a_parameter()
    {
        $this->request->shouldReceive('get')->with('a')->andReturn('a')->once();

        $foo = new Foo($this->request);
        $foo->bar();
    }
}

And this is the tested class:

use Symfony\Component\HttpFoundation\Request;

class Foo
{
    private $request;

    function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function bar()
    {
        $a = $this->request->get('a');
        $b = $this->request->get('b');
    }
}

In the test test_bar_checks_request_a_parameter I would expect bar() method to get 'a' when calling get('a') on the request mock while getting null when calling get('b') instead.

But, instead, we are getting this error:

No matching handler found for Mockery_0_Symfony_Component_HttpFoundation_Request::get("b").

Which seems to say that the Request mock forgot the setUp we made for get('b') call

shouldReceive('get')->with('b')->andReturnNull()->byDefault()

Is this a Mockery limitation? Is a bad approach on our side, a test smell maybe?

Thanks in advance

  • 写回答

1条回答 默认 最新

  • dqlk31541 2014-11-10 13:37
    关注

    It's a Mockery limitation. When you set a new expectation for a method, Mockery disables all the byDefault() expectations for that method, even if they were set with distinct arguments.

    There is an open issue regarding that:

    https://github.com/padraic/mockery/issues/353

    You can solve this by using an array of values and a function that will compute the return value each time. The trick is making the array accesible from the test methods so you can change the return values:

    class FooTest extends \PHPUnit_Framework_TestCase
    {
      private $request;
    
      private $get_return_values = array();
    
      public function setUp()
      {
          $this->request = \Mockery::mock('Symfony\Component\HttpFoundation\Request');
          $this->request->shouldReceive('get')->andReturnUsing(function($arg) {
             return isset($this->get_return_values[$arg]) ? $this->get_return_values[$arg] : null;
          });
      }
    
      public function test_bar_checks_request_a_parameter()
      {
          $this->get_return_values['a'] = 'a';
    
          $foo = new Foo($this->request);
          $foo->bar();
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分