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

前端 未结 2 873
耶瑟儿~
耶瑟儿~ 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:08

    While rdlowrey is correct that with() doesn't make provisions for checking for no arguments passed, the problem doesn't lie with PHPUnit but PHP itself.

    First, if your method doesn't provide default values, the interpreter will raise a fatal error if you don't pass any parameters. This is expected and not entirely relevant to the question at hand, but it's important to state up front.

    Second, if your method does provide default values, calling the method without arguments will cause PHP to alter the call before PHPUnit gets involved to pass the defaults instead. Here's a simple test that demonstrates PHP inserting itself before PHP can check the parameters. It's key to realize that the mock class that PHP creates has the same signature as the mocked class--including the defaults.

    class MockTest extends PHPUnit_Framework_TestCase {
            public function test() {
                    $mock = $this->getMock('Foo', array('bar'));
                    $mock->expects($this->once())
                         ->method('bar')
                         ->with()    // does nothing, but it doesn't matter
                         ->will($this->returnArgument(0));
                    self::assertEquals('foobar', $mock->bar());  // PHP inserts 1 and 2
                    // assertion fails because 1 != 'foobar'
            }
    }
    
    class Foo {
            public function bar($x = 1, $y = 2) {
                    return $x + $y;
            }
    }
    

    This means you can verify that either nothing was passed or the default values were passed, but you cannot be more specific.

    Can you get around this limitation? You can remove default values from arguments when overriding methods, so you should be able to create a subclass and mock it. Is it worth it? My initial gut reaction is that this is a huge code smell. Either your design or your tests are doing the Wrong Thing(tm).

    If you can provide a real-world, concrete example where you actually need to do this kind of test, it's worth spending some time pondering a solution. Until then, I'm satisfied with the purely academic answer of "don't do that." :)

提交回复
热议问题