PHPUnit : Assert a parameter when pass it to mock object

后端 未结 2 649
梦如初夏
梦如初夏 2021-02-18 20:54

For the code below,

$mockObject->expects($this->at(0))
           ->method(\'search\')
           ->with($searchConfig)
           ->will($this-&g         


        
2条回答
  •  野性不改
    2021-02-18 21:32

    You can use Mockery for it

    Say I have a code

    $productValidator = app(ProductValidator:class)->setProduct($productIn);
    

    That would be the test code

    use Mockery;
    
    ...
    
        $productValidator            
            ->shouldReceive('setProduct')->once()
            ->with(Mockery::type(Product::class))
            ->with(Mockery::on(function($productIn) use ($productId) {
                    return $productIn->id === $productId;
                }))
            ->andReturn($productValidator);
    

    Tested on phpunit "version": "8.5.8"

提交回复
热议问题