I'm having a really strange issue when trying to make a HTTP request in one of my tests. My api.php
routes file looks like this:
$router->group(['prefix' => '/v1', 'middleware' => ['auth:api']],
function (\Illuminate\Routing\Router $router) {
$router->apiResource('/contacts', 'ContactsController');
$router->group(['prefix' => '/contacts'],
function (\Illuminate\Routing\Router $router) {
$router->apiResource('/groups', 'Contacts\\GroupsController');
});
});
Running php artisan route:list
shows the following route as being registered
+------------+------------------------+--------------+------------------------------------------------------+----------------+
| Method | URI | Name | Action | Middleware |
+------------+------------------------+--------------+------------------------------------------------------+----------------+
| GET|HEAD | api/v1/contacts/groups | groups.index | App\Http\Controllers\Contacts\GroupsController@index | api auth:api |
+------------+------------------------+--------------+------------------------------------------------------+----------------+
However when I run my test, I get a 404 response. The test is as follows:
public function testICanGetAllOfTheGroups()
{
factory(Group::class)->times(3)->create();
$this->json('GET', '/api/v1/contacts/groups')
->assertStatus(200)->assertJsonCount(3);
}
I have also tried running php artisan route:clear
and php artisan cache:clear
but it hasn't made a difference.
For authentication, I have created the following trait which I am using.
trait Authenticated
{
/** @var \App\Models\User */
protected $user;
public function setUp()
{
parent::setUp();
$this->user = factory(User::class)->create();
Passport::actingAs($this->user);
}
}