Codeception / Stub

Flexible Stub wrapper for PHPUnit's Mock Builder
MIT License
296 stars 17 forks source link

Using trait in Cest gives Too few arguments error #18

Closed mikehaertl closed 1 year ago

mikehaertl commented 5 years ago

I tried to figure out how to use Stubs/Mocks in Cest tests. I assume that the only way to do this is to use the stub trait. So I tried this:

namespace app\tests\unit\base;

use Codeception\Stub\Expected;
use app\models\Session;

class DataPersisterCest
{
    use \Codeception\Test\Feature\Stub;

    public function canGetAndSetSessionProperty($I)
    {
        // Doesn't work
        $sessionStorage = $this->make(Session::class);

        // Doesn't work
        $sessionStorage = $this->make(Session::class, [
            'get' => Expected::never(),
            'set' => Expected::never(),
        ]);

        // ...
    }
}

It always throws an error like this:

1) DataPersisterCest: Construct
 Test  tests/unit/base/DataPersisterCest.php:construct

  [ArgumentCountError] Too few arguments to function Codeception\Actor::__construct(), 0 passed in /usr/local/include/codecept/phpunit/phpunit/src/Framework/MockObject/Generator.php on line 532 and exactly 1 expected  

#1  /usr/local/include/codecept/phpunit/phpunit/src/Framework/MockObject/Generator.php:532
#2  /usr/local/include/codecept/phpunit/phpunit/src/Framework/MockObject/Generator.php:170
#3  PHPUnit\Framework\MockObject\Generator->getMock
#4  /usr/local/include/codecept/codeception/stub/src/Stub.php:527
#5  /usr/local/include/codecept/codeception/stub/src/Stub.php:494
#6  /usr/local/include/codecept/codeception/stub/src/Stub.php:341
#7  /usr/local/include/codecept/codeception/stub/src/Test/Feature/Stub.php:217
#8  app\tests\unit\base\DataPersisterCest->construct
#9  /usr/local/include/codecept/phpunit/phpunit/src/Framework/TestSuite.php:568
#10 /usr/local/include/codecept/codeception/phpunit-wrapper/src/Runner.php:117

Unfortunately the documentation is very unclear. An example there would really help.

righel commented 4 years ago

Having the same issue.

Naktibalda commented 4 years ago

Don't use trait, it was only intented to be used in Test format. Use static Stub::make method as documented in https://github.com/Codeception/Stub/blob/master/docs/Stub.md