First copy the .env.example file and rename it to .env.testing:
Then, execute:
touch test.sqlite
A test.sqlite file will be generated in the D: / laragon / www / laraveauth directory:
Then we modify the database configuration of the .env.testing file to:
DB_CONNECTION=sqlite DB_DATABASE="D:/laragon/www/laraveauth/test.sqlite"
Next execute:
php artisan make: test TaskApiTest
The file is located at:
Next, before modifying this file, execute
php artisan route:list
Take a look at the related routes requested by api task
Let's implement the method of creating a test first:
The method is named relatively easy to understand;
Send a post request to / api / task; the final code is as follows:
public function testCanCreateTask() { $formData = [ 'title' => 'sample test task title', 'description' => 'sample test task description', 'due' => 'next friday', ]; $response = $this->post(route('tasks.store'), $formData); $response->assertStatus(201); }
Since phpunit exists in the vendor \ bin folder:
So: the console executes:
.\vendor\bin\phpunit.bat
result:
Error resolution:
1. APP_KEY is not set in the env.testing file:
Just copy one of the .env files;
Execute again:
.\vendor\bin\phpunit.bat
result:
Without clues, we need this:
$this->withoutExceptionHandling();
Execute again:
.\vendor\bin\phpunit.bat
You can find more error trace information:
The result is that we did not log in before executing the store method:
The revised and execution results are as follows:
Correction method: laragon opens pdo_sqlite extension. [As long as PHP opens the sqlite extension]
Let's add a trait in the class
use RefreshDatabase;
This trait will refresh the database every time the unit is tested. [Execute migration once for migration]
Execute again:
.\vendor\bin\phpunit.bat
The test passed!
Next we assert Json
The result failed:
Modify it and the result still fails:
Modify again:
The test passed!
The rest of the tests are not written.
The documentation is here: Test Guide
The TaskApiTest.php file is as follows:
<?php namespace Tests\Feature; use App\Task; use App\User; use Carbon\Carbon; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\WithFaker; use Tests\TestCase; class TaskApiTest extends TestCase { use RefreshDatabase; protected $user; public function setUp(): void { parent::setUp(); $this->user = factory(User::class)->create(); $this->actingAs($this->user, 'api'); } /** * A basic feature test example. * * @return void */ public function testCanCreateTask() { $due = Carbon::parse('next friday'); $formData = [ 'title' => 'sample test task title', 'description' => 'sample test task description', 'due' => $due, ]; $this->withoutExceptionHandling(); $response = $this->post(route('tasks.store'), $formData); $response->assertStatus(201) ->assertJson(['data' => $formData]); } public function testCanShowTask() { $task = factory(Task::class)->create(); $this->user->tasks()->save($task); $response = $this->get(route('tasks.show', $task->id)); $response->assertStatus(200); } public function testUpdateTask() { $task = factory(Task::class)->create(); $this->user->tasks()->save($task); $due = Carbon::parse('next friday'); $formData = [ 'title' => 'sample test task title', 'description' => 'sample test task description', 'due' => $due, ]; $this->withoutExceptionHandling(); $response = $this->put(route('tasks.update', $task->id), $formData); $response->assertStatus(200) ->assertJson(['data' => $formData]); } public function testDelete() { $task = factory(Task::class)->create(); $this->user->tasks()->save($task); $response = $this->delete(route('tasks.destroy', $task)); $response->assertStatus(200)->assertJson(['message' => 'Success deleted!']); } public function testShowAllTasks() { $tasks = factory(Task::class, 3)->create(); $this->user->tasks()->saveMany($tasks); $response = $this->get(route('tasks.index')); $response->assertStatus(200)->assertJson($tasks->toArray()); } }