1

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. :)

beToiba
  • 163
  • 2
  • 7
  • Please post the code that creates the mock object as well as the code that calls `bla`. – David Harkness Sep 20 '12 at 22:07
  • @David sorry, forgot to add the `bla`-call to the example test method. Now it's structure resembles the one in the real test. I hope the added example of the testObject creation helps. – beToiba Sep 21 '12 at 08:26

1 Answers1

0

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)
David Harkness
  • 35,992
  • 10
  • 112
  • 134