【问题标题】:How to return the argument of a mocked method?如何返回模拟方法的参数?
【发布时间】:2013-05-02 00:39:01
【问题描述】:

大家好,我怎样才能在 PHPUnit 中使模拟方法返回其传递的参数?

例如:

$redirector->expects( $this->once() )->method('gotoUrl')->will( $this->returnValue($argument) );

【问题讨论】:

    标签: php mocking phpunit


    【解决方案1】:

    根据PHPUnit manual

    $stub->expects($this->any())
         ->method('doSomething')
         ->will($this->returnArgument(0));
    

    【讨论】:

    • 谢谢,优化了很多。
    【解决方案2】:
    $redirector->expects($this->once())
               ->method('gotoUrl')
               ->will($this->returnCallback(function() {
                    $args = func_get_args();
                    return $args[0]; // or w/e
                ));
    

    【讨论】:

    • 谢谢,这成功了,而且很酷,它在封闭中。
    • 我已经为 Mockery 搜索了这样的解决方案。像魅力一样工作。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    相关资源
    最近更新 更多