Source: dev.to

TDD CRUD API
CRUD refers to four basic functions: Create, Read, Update, Delete. Test Driven Development (TDD) is a software development approach where test cases are developed to specify and validate what the code will do.

⚠️ IMPORTANT: insert in the controller method a return response like this: return response()->json([ 'status' => true, 'message' => 'User Created Successfully', ], 200); Enter fullscreen mode and now test! public function testCreateUser(){ $response = $this->postJson('/api/users/create', ['name' => 'Sally', 'surname' => 'Willi', 'email' => 's@gmail.com', 'password' => '12345678']); $response->assertStatus(200)->assertJson([ 'message' => 'User Created Successfully', ]); }

Third test - Update User public function testUpdateUser(){ $id = User::all()->first()['id']; $response = $this->put("/api/users/{$id}", ['name' => 'Homer', 'surname' => 'Simpson', 'email' => 'homerS11@gmail.com', 'password' => '12345678']); $response->assertStatus(200)->assertJson([ 'message' => 'User Update Successfully', ]); } if I want to run only this test just run this command: ./vendor/bin/phpunit --filter testUpdateUser tests/Unit/UserControllerTest.php
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners