Create Event controller, migration, model and seed some datas.
php artisan make:test EventTest
Browse to tests/Feature/EventTest.php
We are going to check if Event::all is returning properly with a JSON structure:
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Models\Event;
class EventTest extends TestCase
{
/**
* A basic feature test example.
*/
public function test_example(): void
{
$response = $this->get('/');
$response->assertStatus(200);
}
public function test_for_get_all_events()
{
$events = Event::all();
$resposne = $this->get('/view')->assertStatus(200)->assertJsonStructure(
[
'code',
'message',
'data' =>
[
[
'id',
'name',
'country',
'random_no',
'created_at',
'updated_at'
],
],
]
);
}
}
Function which is being tested:
public function view(): String
{
$a = Event::all();
return json_encode([
'code' => 200,
'message' => 'Successful',
'data' =>$a
]);
}
To run tests exec: php artisan test
The function's Json structure matches with the Test's JSON structure hence the test passes
Top comments (0)