开发者

How to set return value when using the test_helpers extension in PHPUnit

I've recently been given the task of getting our system set up for unit testing b开发者_如何学Pythonut there's one problem I'm having. The example at http://sebastian-bergmann.de/archives/885-Stubbing-Hard-Coded-Dependencies.html is setup very similar to the code I'm trying to test. The only other thing that I need to do is to have doSomethingElse() return something of my choosing.

UPDATE

I have tried setting the getMock to some variable and then setting the return value. But PHPUnit doesn't seem to notice the variable and the return value I've set. This is because, I'm assuming, that the variable is never used anywhere. But with how the code is set up, there is no way to pass the mock object into foo so it knows to use that instead of the actual Bar class. I've added in the Foo and Bar class for easier reference. The original Bar class returns * but I want it to return ** from the mock class.

Using set_new_overload() isn't mandatory for this test but so far using it is the only way I've been able to successfully mock Bar.

<?php
require_once 'Foo.php';

class FooTest extends PHPUnit_Framework_TestCase {

    protected function setUp()
    {
        $mock = $this->getMock(
            'Bar',                     /* name of class to mock     */
            array('doSomethingElse'),  /* list of methods to mock   */
            array(),                   /* constructor arguments     */
            'BarMock'                  /* name for mocked class     */
        );

        $mock->expects($this->any())
                 ->method("doSomthingElse")
                 ->will($this->returnValue("**")
        ); 
        set_new_overload(array($this, 'newCallback'));
    }

    protected function tearDown()
    {
        unset_new_overload();
    }

    protected function newCallback($className)
    {
    switch ($className) {
        case 'Bar':
                    return 'BarMock';
        default:
                    return $className;
        }
     }

    public function testDoSomething()
    {
        $foo = new Foo;
        $this->assertTrue($foo->doSomething());
    }
}
?>


<?php
require_once 'Bar.php';

class Foo {
    public function doSomething()
    {
        // ...

        $bar = new Bar;
        $bar->doSomethingElse();

        // ...

        return TRUE;
    }
}
?>



<?php
class Bar {
    public function doSomethingElse()
    {
        return '*';
    }
}
?>


The mock class won't have any expected behavior by itself. To use it as a stub (do X when method Y is called) you need access to the BarMock instance created, but it doesn't look like the new overload will give you that.

Instead I would create a custom subclass of Bar that returns what you want from doSomethingElse().


I don't really understand what you're trying to do here, but:

  • You're calling getMock() but not actually assigning to anything
  • To set a return value, see Example 11.2: http://www.phpunit.de/manual/3.5/en/test-doubles.html
  • You have some odd whitespace in ' BarMock'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜