1

我的 PHPUnit 测试有问题。我有这样的方法:

public function bla() 
{
    $this->blub();
}

$_testObject 是 Foo 的一个模拟实例,如下所示:

public function setUp()
{
    $this->_testObject = $this->getMockBuilder('Foo')
                              ->setMethods(array('blub'))
                              ->getMock();
}

我的测试方法是这样的:

/**
 * @test
 * @covers Foo::bla
 */
public function shouldCallBlubOnce() 
{
    $this->_testObject->expects($this->once())
                      ->method('blub');

    //forgot this one :D
    $this->_testObject->bla();
}

该测试的结果提供:

PHPUnit_Framework_ExpectationFailedException : 
Expectation failed for method name is equal to <string:blub> when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.

提前致谢。:)

4

1 回答 1

0

以下测试使用 PHPUnit 3.6.12 在我的系统上通过。

class Foo {
    function bla() {
        $this->blub();
    }

    function blub() {
        echo 'blub';
    }
}

class FooTest extends PHPUnit_Framework_TestCase {
    function setUp() {
        $this->_testObject = $this->getMockBuilder('Foo')
                                  ->setMethods(array('blub'))
                                  ->getMock();
    }

    /**
     * @test
     * @covers Foo::bla
     */
    public function shouldCallBlubOnce() 
    {
        $this->_testObject->expects($this->once())
                          ->method('blub');
        $this->_testObject->bla();
    }
}

运行测试会产生以下结果:

PHPUnit 3.6.12 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 12.50Mb

OK (1 test, 1 assertion)
于 2012-09-21T18:15:54.853 回答