It seems like the issue you're encountering is related to the Laravel application not being booted properly within your test environment, which is why the config helper is not available.
To resolve this, you should ensure that your base TestCase class is setting up the application correctly. Since you're using Orchestra Testbench, you're on the right track. However, you might need to make sure that you're using the correct namespace for your TestCase class in your Pest tests.
Here's a step-by-step guide to ensure your testing environment is configured correctly:
-
Verify that your
TestCaseclass is in the correct namespace and that it extendsOrchestra\Testbench\TestCase. It looks like you've done this correctly. -
In your
Pest.phpfile, make sure you're using the correct namespace for yourTestCase. It should match the namespace of yourTestCaseclass. For example, if yourTestCaseis in theAbkrim\ApiSage\Testsnamespace, yourPest.phpshould look like this:
<?php
uses(Abkrim\ApiSage\Tests\TestCase::class)->in('Unit');
- In your test file (e.g.,
DemoTest.php), you don't need to use theTestCaseclass again since you've already specified it inPest.php. You can remove the->uses(TestCase::class)part from your tests:
<?php
test('confirm environment is set to testing', function () {
expect(config('app.env'))->toBe('testing');
});
test('sum', function () {
$result = 3;
expect($result)->toBe(3);
});
- Ensure that your
TestCaseclass is bootstrapping the application correctly. You might want to add asetUpmethod to yourTestCaseclass to boot the application:
<?php
namespace Abkrim\ApiSage\Tests;
use Orchestra\Testbench\TestCase as Orchestra;
use Abkrim\ApiSage\ServiceProvider;
class TestCase extends Orchestra
{
protected function getPackageProviders($app): array
{
return [
ServiceProvider::class,
];
}
protected function setUp(): void
{
parent::setUp();
// Perform environment setup
}
}
- Finally, make sure that your test classes are autoloaded correctly by Composer. You should have an
autoload-devsection in yourcomposer.jsonthat looks something like this:
"autoload-dev": {
"psr-4": {
"Abkrim\ApiSage\Tests\": "tests/"
}
}
After making these changes, run composer dump-autoload to regenerate the autoload files.
Now, try running your tests again. If everything is set up correctly, your tests should be able to use the config helper and any other Laravel features you need.