Hi @munazzil, you didn't get my question. I know the Artisan command available to list the routes....
I want to write a PHPUnit test that ensures that the "Given Route hits the Specified Controller Action (Method) and it has the Specified middlewares applied"
/** @test */
public function userCanAccessDashboard()
{
$user = factory(User::class)->create();
$response = $this->actingAs($user)
->get('/dashboard');
$response->assertStatus(200);
$response->assertViewHas(['item_from_view', 'another_item_from_view']);
}
/** @test */
public function guestCanNotVisitLoggedInPages()
{
$response = $this->get('/dashboard');
$response->assertRedirect('/login');
}
I think this meets your criteria. You can be sure that the controller action is hit by confirming that variables it creates and passed to the view (assuming it returns a view) end up in the view, i.e.
Hi @mstrauss , Your Answer is good for checking the Application Feature about getting the correct view and ensuring that the auth Middleware is applied.
I want to test the URL Route is being Handled by the Expected Controller Action (Irrespective of the Action Logic / Implementation). To be very precise, I want to hook the Test into the Laravel Routing layer and ensure that the correct Action method is invoked.
Goal / Motivation
Write a Loosely Coupled Test to check the Route calls the expected Action method.
@kingmaker_bgp I see what you are saying, but in essence, you are testing the Laravel Framework at that point, which is already thoroughly tested by many excellent developers. Your tests should test the unique things that your application does, not the Framework (IMO).
Yes @mstrauss, I didn't wish to test the Laravel Framework, But my concern is
I might accidentally change the routes/web.php file in the Future and the correct Controller Action may not be called.... I would like to prevent this using a Test for the Defined Routes.
This will tell exactly the Error and I don't want to reverse engineer other Failing Tests.