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

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners