I created a feature test api.
CategoryApiTest.php
namespace Tests\Feature;
use App\Models\Category;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\JsonResponse;
use Tests\TestCase;
class CategoryApiTest extends TestCase
{
use RefreshDatabase;
protected $faker;
public function setUp(): void
{
parent::setUp(); // TODO: Change the autogenerated stub
$this->user = $this->faker = Category::factory()
->create([
'name' => 'Food',
'description' => 'This is the food category',
'menu' => true,
'image' => 'IMG_4985',
'parent_id' => '1'
]);
}
...
public function testUpdateCategory()
{
$response = $this->putJson('/api/categories/1', [
'name' => 'Water Update',
'description' => 'This is the water category update',
'menu' => true,
'image' => 'IMG_56898',
'parent_id' => '1'
]);
$response->assertStatus(JsonResponse::HTTP_OK)
->assertJsonFragment([
'name' => 'Water Update',
'slug' => 'water-update',
'parent_id' => 1
]);
}
...
}
This case always return fail, because cannot find category with id = 1
.
I checked list categories and I see:
I create file .env.testing
and database_test but I think phpunit.xml
doesn't use file .env.testing
so RefreshDatabase
not working.
Data test does not refresh
. Something wrong, please help me fix it.