ssquare's avatar

Simple store test getting failed

I am trying to learn a test in laravel, and I have written a simple test to test the store action. But, it is showing error as :

  • Tests\Feature\CustomerTest > create customer success
   Illuminate\Database\QueryException 

  SQLSTATE[HY000]: General error: 1 no such table: users (SQL: insert into "users" ("name", "email", "email_verified_at", "password", "remember_token", "updated_at", "created_at") values (Florencio Kertzmann, [email protected], 2021-06-14 06:46:01, yIXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi, unbD4TMKs1, 2021-06-14 06:46:01, 2021-06-14 06:46:01))

What is the reasons of this test getting failed?

public function test_create_customer_success()
{
    $user = User::factory()->create();
    $this->actingAs($user, 'sanctum');

    $customerData = Customer::factory()->create();

    $this->json('POST', 'api/customers', $customerData, ['Accept' => 'application/json'])
        ->assertStatus(201)
        ->assertJson([
            "message" => __('crud.created')
        ]);
}

For the testing environment, I am setting as follows on phpunit.xml

         <server name="DB_CONNECTION" value="sqlite"/>
         <server name="DB_DATABASE" value=":memory:"/>
0 likes
1 reply
Sergiu17's avatar
Sergiu17
Best Answer
Level 60

add RefreshDatabase trait to your class

use Illuminate\Foundation\Testing\RefreshDatabase;

class YourTestClass
{
	use RefreshDatabase;

	public function test_create_customer_success()
	{
		// ..

Please or to participate in this conversation.