Ever since I updated from Laravel 9 to 12 (One version at a time), some of my tests are failing. I have no idea why, but for some reason they're returning 302 a redirect to /. I've tried disabling middleware that may be causing this, but that doesn't help. Anyone have any idea how I can figure out what's going on here? The admin.downloads.store works properly - it stores the data in the db properly, where it fails is the admin.downloads_groups.edit route asserting status 200 - it returns 302 and the redirect is to / for some reason instead of /login.
public function test_can_view_edit_individual_download($values) {
Storage::fake('local');
$response = $this->postJson(route('admin.downloads.store'), $values);
$downloadGroup = DownloadsGroup::first();
$response = $this->get(route('admin.downloads_groups.edit', $downloadGroup->id));
$response->assertStatus(200);
}
Setup:
public function setUp(): void {
parent::setUp();
$this->setupAdmin();
}
@jarcas You shouldn’t be making multiple requests in a single test case like that. It’s not supported and can lead to unexpected side effects because the framework isn’t re-booted between requests like it would in a production environment.
Use factories to create records instead:
public function test_can_view_edit_download_group_form(): void
{
$downloadGroup = DownloadGroup::factory()->create();
$response = $this->get(route('admin.download_groups.edit', $downloadGroup));
$response->assertOk();
}
Test cases are called test cases because they should test a single case instead of having multiple concerns like creating a record and then retrieving it.
@martinbean Thanks, fixed that but I'm still getting a 302 for some reason. While I was updating the code to use a factory, I forgot to assign a column and got an error from the view in the controller's method I'm trying to access, but once I got the factory working properly I'm getting the 302 again.
With this the route kinda works - it returns the view, however $downloadsGroup is null. It's set correctly in the web.php routes, and the controller. Is there some middleware that handles passing parameters to routes that I'm missing?