D
D
Dmitry Baskakov2022-04-19 14:26:38
Laravel
Dmitry Baskakov, 2022-04-19 14:26:38

Why is a record deleted from the database during tests?

There is this test:

public function test_isset_user_in_database()
    {
        $this->post('/api/v2/token/get', ['google_id' => $this->getGoogleID()]);
        $this->assertDatabaseHas('app_users', ['google_id' => $this->getGoogleID()]);
    }


The test does not use RefreshDatabase.

The setUp method is like this:
protected function setUp(): void
    {
        parent::setUp();

        $response = $this->post('/api/v2/token/get', ['google_id' => $this->getGoogleID()]);
        $responseJSON = $response->json();
        $this->setJWTToken($responseJSON['data']['token']);
    }


The entry is created like this:
public function getOrCreateByGoogleID(string $googleID): ?AppUserModel
    {
        try {
            return $this->model::firstOrCreate(['google_id' => $googleID]);
        } catch (QueryException $e) {
        }

        return null;
    }


Why does the created entry disappear when checking with $this->assertDatabaseHas ? Moreover, other records are saved
. And with requests from the postman, everything works as it should

. UPD: the problem occurs if you run more than 1 test, and if this test is not the first

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Baskakov, 2022-04-19
@dmitrybascacov

Somehow everything got fixed
Maybe it's Laravel's request limits or the cache. At some point, I indicated in the test config to separately make php processes for each test.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question