For context, I'm working on a RNG-based motorsport simulator, for lack of a better term. Users can create universes (think FIA in real life terms), in a universe they can create series (think F1, F2, F3 etc) and in each series they can create seasons.
I'm currently working on improving test coverage but I'm now getting a LazyLoadingViolationException that I can't explain, since I only get the exception in my test and not when browsing normally.
The test;
/** @test */
public function theIndexPageShowsAllSeasonsForTheSelectedSeries()
{
$user = User::factory()->create();
$series = $this->createSeriesForUser($user);
Season::factory(5)->for($series)->create();
$this->actingAs($user)
->get(route('series.seasons.index', [$series]))
->assertOk()
->assertInertia(fn(Assert $page) => $page
->component('Seasons/Index')
->has('series.seasons', 5)
);
}
createSeriesForUser() (inside Laravel's TestCase class);
protected function createSeriesForUser(User $user): Series
{
return Series::factory()->for(Universe::factory()->for($user)->create())->create();
}
The route is a resource controller for the Season model, and calls the index method;
public function index(Series $series): Response
{
return Inertia::render('Seasons/Index', [
'series' => $series->load(['seasons' => fn(HasMany $query) => $query->orderBy('year')]),
'can' => [
'edit' => Gate::check('owns-universe', $series->universe),
],
]);
}
The exact exception is Illuminate\Database\LazyLoadingViolationException: Attempted to lazy load [series] on model [App\Models\Season] but lazy loading is disabled., which tells me, somewhere, the series method is called on a Season object, which doesn't happen anywhere in the code that's actually called.
Adding
protected $with = [
'series',
];
to the Season model fixes the exception, which makes no sense to me since in the entire application, the series relationship on the Season model is called only in tests, and unrelated Vue components.
I've got two questions regarding this issue;
- why am I only getting this error when running this specific test (other tests and visiting the page normally are fine)
- what's causing the exception, when at no point the relationship is called?