freinn
freinn

Reputation: 1079

How to properly use atLeastOnce method in Codeception\Util\Stub?

I'm using codeception for testing my PHP app, and there is a method I don't know how to use. It's called Stub::atLeastOnce() and, like Codeception's documentation of the Stub class says:

"Checks if a method has been invoked at least one time. If the number of invocations is 0 it will throw an exception in verify."

But when I try to use it, doesn't matter that I comment the call to User::getName() or not, the test passes.

My user class looks like this:

<?php

class User {
  public function getName() {
    return 'pepito';
  }

  public function someMethod() {

  }
}  

And my test method like this:

public function testStubUsage() {
    // all methods that the stub impersonates must be, at least, defined
    $user = Stub::make('User', array('getName' => Stub::atLeastOnce(function() { return 'Davert'; }), 'someMethod' => Stub::atLeastOnce('User::getName')));
    $user->getName();
}

So, what is the usage of that function to make the test fail if User::getname() is never called?

Upvotes: 2

Views: 503

Answers (1)

freinn
freinn

Reputation: 1079

The doc is not correct, you have to pass $this as third argument of Stub::make() for this to work properly:

public function testStubUsage() {
    $user = Stub::make('User', array('getName' => Stub::atLeastOnce(function() { return 'Davert';}), 'someMethod' => function() {}), $this); // <- Here
    $user->getName();
}

Upvotes: 4

Related Questions