I need to use the DB facade within the tests, however I am getting the errorA facade root has not been set.
I have tried to load the facade in a TestCase extension as follows:
protected function getPackageAliases($app)
{
return [
'DB' => 'Illuminate\Support\Facades\DB'
];
}
protected function getEnvironmentSetUp($app)
{
$app->withFacades();
}
However, I still keep getting the same error. Given the structure of the application, I require a connection to the database, using mockery is not an option.
After reviewing the Laravel code in more detail, I realized that the default TestCase used the "
CreatesApplication
" trait, which is responsible for initializing the application.In my tests in order to reuse a common object for all tests, I was overriding the constructor, always calling the parent constructor, but it seems that the application release is available later, so using classes that use facades in the constructor of a test suit will give an error for the facades.
The solution
Remove elements that require facades from the constructor and call them directly within each test function, by which time the application and the facades will be available without problems.