dongyi2534 2013-11-19 17:25
浏览 37

期望PHPUnit中的变量参数列表

I have an adapter that has a method taking a variable list of arguments and forwards it to a method that takes those same arguments in a framework I am using. I want to test that my adapter correctly forwards the arguments. I however do not want my test to know about which kind of arguments the framework supports.

I have a working expects as follows:

$context->expects( $this->once() )
    ->method( 'msg' )
    ->with(
        $this->equalTo( $someMessageArguments[0] ),
        $this->equalTo( $someMessageArguments[1] ),
        $this->equalTo( $someMessageArguments[2] )
    );

This is clearly not good as it assumes the length of the variable list is 3. I want to use a data provider and test with different lengths as well, in which case this code won't cut it.

Is there a sane way to do this via the PHPUnit API? I hacked this up, which also works, though seems quite evil as well:

$invocationMocker = $context->expects( $this->once() )
    ->method( 'msg' );

$invocationMocker->getMatcher()->parametersMatcher
    = new PHPUnit_Framework_MockObject_Matcher_Parameters( $someMessageArguments );

In case there is no way to do this nicely via the PHPUnit API, is there an approach that is better then the two listed here?

  • 写回答

2条回答 默认 最新

  • doubi6303 2013-11-20 23:45
    关注

    I haven't found the way to do it with PHPUnit mocks.

    Using Mockery, you can archieve that:

    $mock->shouldReceive('msg')
         ->once()
         ->withArgs($someMessageArguments);
    

    Personally I normally prefer Mockery over PHPUnit mocks.

    https://github.com/padraic/mockery

    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题