phpspec / phpspec

SpecBDD Framework for PHP

Home Page:http://www.phpspec.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mocking issue

bogdan-dubyk opened this issue · comments

I need to make a mock like this

    $logger->info(
            'Invalid',
            [
                'exception' => new ValidationException('some error'),
                'survey' => $survey
            ]
        )->shouldBeCalled();

But when I run the actual test, it's failing with a error Unexpected method call on.... (I can't show the full exception as output is huge...), but prediction has same data as actual call (same log message, survey data and exception message).

Looks like it failing while compare ValidationException object. Is there a way of fixing this?? I know I had something similar with a phpunit, but https://github.com/hamcrest/hamcrest-php helped with that, not sure how to fix it here

I guess this is really a phpspec/prophecy question, but I can answer here

The default in an expected method call is that values are matched on identity (i.e. ===) which is why the objects do not match

However, in Prophecy you can instead provide an Argument object in that place that changes how the arguments are compared (see https://github.com/phpspec/prophecy#arguments-wildcarding)

in your case you can make the array comparison looser (i.e. ==) by wrapping in Argument::exact():

use Prophecy\Argument;

//..

$logger->info(
    'Invalid',
    Argument::exact([
        'exception' => new ValidationException('some error'),
        'survey' => $survey
    ])
)->shouldBeCalled();

You can also use this to be more specific about what aspects of the method call you are targeting, e.g. if writing a test about the specific wording of the message string you might use:

$logger->info(
    'Something very specific',
    Argument::any()
)->shouldBeCalled();