It sounds like you're encountering an issue where your tests are not catching lazy loading issues that occur when Model::shouldBeStrict(); is enabled in your AppServiceProvider. This is likely because your tests are not running with the same configuration as your production or development environments.
To ensure that your tests catch lazy loading issues, you need to make sure that the shouldBeStrict mode is enabled during testing. You can do this by setting up your test environment to mimic your production or development environment as closely as possible.
Here's what you can do:
-
Ensure that
Model::shouldBeStrict();is called during the setup of your tests. You can do this in thesetUpmethod of your test class or in a test-specific service provider. -
Make sure that your
.env.testingfile (or whichever environment file your tests are using) does not override settings that would disable strict loading. -
If you're using Laravel's built-in testing features, such as
RefreshDatabase, ensure that they are not interfering with the strict loading settings.
Here's an example of how you might set up a test to ensure shouldBeStrict is enabled:
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Model;
use Tests\TestCase;
class ExampleTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
// Enable strict loading for all tests
Model::shouldBeStrict();
}
/** @test */
public function it_catches_lazy_loading()
{
// Your test code here
}
}
By doing this, you're ensuring that any lazy loading performed by your models will throw an exception during testing, just as it would in your production or development environment.
If you're still not catching lazy loading issues, you may want to review your test cases to ensure they are comprehensive enough to cover the scenarios where lazy loading might occur. Remember that tests can only catch issues that they are designed to test for, so it's important to have thorough test coverage for your application's functionality.