I have a simple test that checks that the dashboard is renderable for authenticated users.
Here is the code:
<?php
namespace TestsFeature;
use AppModelsUser;
use TestsTestCase;
class DashboardTest extends TestCase
{
public function test_dashboard_page_can_be_rendered()
{
$this->actingAs(User::factory()->create());
$response = $this->get('/dashboard');
$response->assertStatus(200);
}
}
This code works fine when I run php artisan test
.
Output:
PASS TestsFeatureDashboardTest
✓ dashboard page can be rendered
But, when running this on GitHub Actions, I get the following error:
There was 1 failure:
1) TestsFeatureDashboardTest::test_dashboard_page_can_be_rendered
Expected response status code [200] but received 302.
Failed asserting that 200 is identical to 302.
/home/runner/work/material-hub/material-hub/vendor/laravel/framework/src/Illuminate/Testing/TestResponse.php:198
/home/runner/work/material-hub/material-hub/tests/Feature/DashboardTest.php:17
2
Answers
I missed passing a middleware that I used to group all routes. It must be passed otherwise redirect will occur as described error.
That middleware is passed now and all tests run as usual.
302 means that you are redirected to another page. I would assume that the reason this happens is that the user you are creating is not used or authenticated to go to the requested route.
You can try a similar approach by twisting your code a bit:
That way you ensure that the user you have created will be the one to access the route you want.
In the
actingAs
you can also use a second parameter to define specifically the guard you want to use for authentication, for example: