mercredi 10 mars 2021

feature test for http restful api on laravel 8

I'm trying to write a feature test in RESTful API that I have been created in laravel 8. I have ChatController that gets user chats by getUserChats method:

// get chats for user
public function getUserChats()
{
    $chats = $this->chats->getUserChats();
    return ChatShowResource::collection($chats);
}

that called by this route:

Route::get('chats', [
    App\Http\Controllers\Chats\ChatController::class, 'getUserChats'
]);

this is my feature test on this route:

public function test_get_user_chats()
{
    // creates the fake user
    $user = User::factory()->create();
    $this->actingAs($user, 'api');

    // adds the chat to new fake user that created
    $response_create_chat = $this->json('POST', '/api/chats', [
            'recipient' => $user->username,
            'body' => 'some chat text',
        ]
    );

    $response_create_chat
        ->assertStatus(201)
        ->assertJsonPath('data.body', 'some chat text');

    $response_get_chats = $this->json('get', '/api/chats');

    $response_get_chats->assertStatus(200);
}

the test runs successfully by this green output OK (1 test, 3 assertions) but I don't know this is the best way to implementing that.

so the question is that anything else I have to do in my test? am I doing the right feature test?

Aucun commentaire:

Enregistrer un commentaire