I got a problem with my PHPUnit test. I have a method like:

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

$_testObject is a mocked instance of Foo, like this:

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

My test method is like:

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

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

Result of this test delivers:

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.

Thanks in advance. :)

有帮助吗?

解决方案

The following test passes on my system using 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();
    }
}

Running the test produces this:

PHPUnit 3.6.12 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 12.50Mb

OK (1 test, 1 assertion)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top