It looks like the test is failing because the expected redirect URL does not match the actual URL after the destroy method is called. The test expects a redirect to 'http://localhost/schools', but it's actually being redirected to 'http://localhost/login'. This usually happens when the route you're trying to access is protected by authentication middleware and the user is not authenticated in the test environment.
To fix this, you need to ensure that the user is authenticated before making the request in your test. You can use the actingAs method to authenticate a user in your test. Here's how you can modify your test:
it('shows that clicking the remove button removes the school', function (){
// Arrange
\App\Models\Teachers\Country::factory()->create([
'name' => 'United States',
'abbr' => 'US',
]);
\App\Models\Geostate::factory()->count(50)->create();
\App\Models\Teachers\County::factory()->count(25)->create();
$user = \App\Models\User::factory()
->has(\App\Models\School::factory()->count(1))
->create();
// Act & Assert
$this->actingAs($user)
->get(route('schools.remove', ['school' => $user->schools->first()]))
->assertRedirect(route('schools.index'))
->assertSessionHasNoErrors(); // This line ensures there are no session errors, such as authentication errors.
// You may also want to assert that the school was actually removed
$this->assertDatabaseMissing('schools', ['id' => $user->schools->first()->id]);
});
Please note that I've made the following changes:
- Used
$this->actingAs($user)to authenticate the user before making the request. - Changed the assertion to use
route('schools.index')instead of the string'schools'to ensure that the correct route name is used. - Added an assertion to check that there are no session errors.
- Added an assertion to check that the school was actually removed from the database.
Make sure that the School model is imported correctly at the top of your test file if it's namespaced differently.
Also, ensure that your route names are consistent. In the test, you're using 'teachers.schools.remove', but in the web routes, you've defined it as 'schools.remove'. Make sure to use the correct route names as defined in your web.php.