farshadf's avatar

laravel show unit test is not asserting.

i have a laravel application that i want to write tests for now when i run show test its not working and i am writing this test for my api btw : so my test is like below :


public function testShow()
{
    $response = $this->json('GET', '/api/roomfacility/{roomfacility}', []);

    $response->assertStatus(200);

}

but i get this error :



    20) Tests\Feature\RoomCapacityHistoryTest::testShow
Expected status code 200 but received 404.
Failed asserting that false is true.

i want to know what is the error here because when i try it in browser it works and i want to know how to test my crud api , i want to test my index,show,create,update for my api i know there are some tutorials about the testing but they are not about the api .

0 likes
1 reply
tykus's avatar
tykus
Best Answer
Level 104

This '/api/roomfacility/{roomfacility} is likely not a valid URL; expect it is your Route definition with wildcard.

public function testShow()
{
    $roomFacility = factory(RoomFacility::class)->create();

    $response = $this->json('GET', '/api/roomfacility/' . $roomFacility->id, []);

// or

    $response = $this->json('GET', route('roomfacility.show', $roomFacility), []);

    $response->assertStatus(200);

}
1 like

Please or to participate in this conversation.