Stub icon indicating copy to clipboard operation
Stub copied to clipboard

Way to test method call with params

Open antonmarin opened this issue 9 years ago • 3 comments

What are you trying to achieve?

Trying to test if method called with my params using Stub utility.

$db = Stub::make(
            Connection::className(), 
            [
                'quoteColumnName' => Stub::exactly(2, function($params){
                    return $params[0];
                }),
            ],
            $this
        );

What do you get instead?

No way or documentation to do it

Provide console output if related. Use -vvv mode for more details.

# paste output here

Provide test source code if related

// paste test

Details

  • Codeception version:
  • PHP Version:
  • Operating System:
  • Installation type: Phar || Composer
  • List of installed packages (composer show)
  • Suite configuration:
# paste suite config here

antonmarin avatar Aug 01 '16 13:08 antonmarin

If Stub can't do it, just use $this->createMock() instead.

Naktibalda avatar Aug 01 '16 14:08 Naktibalda

Tried, but it doesn't work right if i use Stub too and if use only PhpUnit mockery, then code becomes too long and difficult to read.

antonmarin avatar Aug 03 '16 07:08 antonmarin

@antonmarin I was able to implement this using the following technique:

Stub::exactly(2, function ($params) use ($I, $expected) {
    $I->assertEquals($expected, $params);
    return $params[0];
});

Would be nice to have a handy expectation builder though.

Kolyunya avatar Jul 21 '21 14:07 Kolyunya