Check that mock's method is called without any parameters passed (in phpunit)

前端 未结 2 977
耶瑟儿~
耶瑟儿~ 2021-01-04 07:06

In phpunit we can specify the method was called with particular

->with($this->equalTo(\'foobar\'))

or any

->with($         


        
2条回答
  •  长情又很酷
    2021-01-04 07:17

    PHPUnit mock objects can only use the ->with() constraint method to verify that the count or respective values of passed parameters match those passed to the mocked method when invoked. You can't use it to require that no arguments are passed to the mock method.

    The mock verification process specifically checks that the passed parameter count and the associated values are compatible with those passed to the with constraint. Also, as you've probably seen, specifying the with constraint without any values won't work either; if with receives no parameters it won't add any parameter constraints for verification.

    You can see the actual PHPUnit_Framework_MockObject_Matcher_Parameters::verify method used to verify mock method parameters in the linked github source.

    If validation of no passed arguments is a requirement you'll need to specify your own mock class to verify such a condition outside the PHPUnit mocking capabilities.

提交回复
热议问题