Илья Шах
Илья Шах

Reputation: 117

How can I check that a method has been called with a certain argument with Codeception?

I need to check that a method has been called with a certain argument. With Expected::once() I can check that a function has been called, but can only specify the expected return value, not the argument. I could not find a way to do this in the Codeception docs. Does Codeception have a method for this?

public function testSomething()
{
    $conversation = $this->make('Class', [
        'ask' => \Codeception\Stub\Expected::once('this string will be returned')
    ]);
    $conversation->run();
}

Upvotes: 3

Views: 872

Answers (2)

Илья Шах
Илья Шах

Reputation: 117

I found a solution. It is PHPUnit syntax, but it works! We can test that the ask() method was called once with 2 params: 'first param' and 'second param'. Maybe someone has a more elegant solution?

public function testSomething()
{
    $conversation = $this->make('Class', [
        'ask' => \Codeception\Stub\Expected::once()
    ]); 

    $conversation->expects($this->exactly(1))
        ->method('ask')
        ->with('first param', 'second param');

    $conversation->run();
}

Upvotes: 3

Tanguy Bernard
Tanguy Bernard

Reputation: 274

With codeception I'm using something like this :

$conversation = Stub::make(MyClass::class,
            [
                'ask' => Stub\Expected::exactly(1, function ($params)  {
                    $this->assertEquals('yourFirstParam', $params[0]);

                })
            ]
        );

Upvotes: 1

Related Questions