Skip to content

Instantly share code, notes, and snippets.

@mvriel
Created September 4, 2014 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvriel/110b100261847a7ccb62 to your computer and use it in GitHub Desktop.
Save mvriel/110b100261847a7ccb62 to your computer and use it in GitHub Desktop.
Why does my test not fail on the second mockedMethod call?
{
"autoload": {
"psr-0": { "": "src/" }
},
"require": {
},
"require-dev": {
"phpunit/phpunit": "3.7.*",
"mockery/mockery": ">=0.8",
}
}
<?php
class MyClass
{
public function test($dependency)
{
$dependency->mockedMethod('I have random arguments');
$dependency->mockedMethod(
array(
'choices' => array(
'Y' => 'YES',
'N' => 'NO',
'A' => 'ALWAYS',
)
)
);
}
}
<?php
include 'vendor/autoload.php';
include 'MyClass.php';
use Mockery as m;
class MyClassTest extends \PHPUnit_Framework_TestCase
{
/** @var MyClass */
private $fixture;
/**
* Initializes the fixture for this test.
*/
public function setUp()
{
$this->fixture = new MyClass();
}
/**
* @covers AutoTrack\InventoryBundle\Form\Type\ApkType::setDefaultOptions
*/
public function testIfCorrectDefaultsAreSet()
{
$mock = m::mock('stdClass');
$mock->shouldReceive('mockedMethod')->with(
array(
'choices' => array(
'Y' => 'YESH',
'N' => 'NO',
'A' => 'ALWAYS',
)
)
);
$mock->shouldReceive('mockedMethod')->withAnyArgs();
$this->fixture->test($mock);
}
}
@robertbasic
Copy link

I made 2 changes to make the test fail:

Added phpunit.xml to include the Mockery listener:

<phpunit colors="true">
    <listeners>
        <listener class="\Mockery\Adapter\Phpunit\TestListener"></listener>
    </listeners>
</phpunit>

And added call count validation to the mocked methods:

public function testIfCorrectDefaultsAreSet()
{
    $mock = m::mock('stdClass');

    $mock->shouldReceive('mockedMethod')
        ->once()
        ->with(
        array(
            'choices' => array(
                'Y' => 'YESH',
                'N' => 'NO',
                'A' => 'ALWAYS',
            )
        )
    );
    $mock->shouldReceive('mockedMethod')
        ->once()
        ->withAnyArgs();

    $this->fixture->test($mock);
}

And now the tests fail with:

There was 1 error:

1) MyClassTest::testIfCorrectDefaultsAreSet
Mockery\Exception\InvalidCountException: Method mockedMethod(array('choices'=>'array(...)',)) from Mockery_0_stdClass should be called
 exactly 1 times but called 0 times.

Also the order of expectations set should not matter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment