I am beginning to use Codeception for Unit testing within Yii2. Great that Yii2 embraces it completely now, a major reason for me to upgrade!
I am having difficulties letting Codeception find the classes. Controller classes that I have written e.g. These classes load fine by autoloading in the Yii2 application.
E.g. this controller:
<?php
class RecipeControllerTest extends \Codeception\TestCase\Test
{
/**
* @var \UnitTester
*/
protected $tester;
protected function _before()
{
}
protected function _after()
{
}
// tests
public function testMe()
{
echo \app\controllers\RecipesController::getallrecipes();
}
}
Results when I do codeception run unit
in the console in his output:
FATAL ERROR. TESTS NOT FINISHED.
Class '
app\controllers\RecipesController
' not found
My _bootstrap.php
file includes this:
require_once(__DIR__ . '/../../vendor/autoload.php');
require_once(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
So I am sure I'm doing something wrong but am clueless.
I solved this problem by explicitly creating the Yii application in the global Codeception bootstrap file.
That is, inside tests/_bootstrap.php
, appended the following line of code:
new yii\web\Application(require(__DIR__ . '/../config/test.php'));
Most possibly it's because only in Application
constructor the autoloaders are being actually attached to the runtime.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With