1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Laravel RefreshDatabase trait not wrapping each test in a transaction

Discussão em 'Outras Linguagens' iniciado por fudo, Novembro 6, 2024 às 09:43.

  1. fudo

    fudo Guest

    RefreshDatabase docs states that


    The Illuminate\Foundation\Testing\RefreshDatabase trait does not migrate your database if your schema is up to date. Instead, it will only execute the test within a database transaction.

    I'm on Laravel 10 and I think it is not working properly in my project.

    Problem #1


    Since the project migrations take ~1m to be executed (php artisan migrate:fresh --env testing) I have a local testing db that I keep up to date to run tests quickly, but tests execution always took at least 1m as if it is running all the migrations before running the tests.

    Problem #2


    I need to test that a piece code falls into a catch(Exception $e) branch and since this piece of code retrieve results from the db I thought to alter the table name after all the setUp to make it throw an exception, and that worked.
    Problem is, now every subsequent test in the same test class that tests the same piece of code fails with the message SQLSTATE[42S02]: Base table or view not found: this means the table still have the new wrong name, so it looks like it is not the single test to be wrapped in a migration but all the tests in the same test class, but this conflicts with the previous db operation (e.g. instances being created) not affecting subsequent tests.

    I really cannot figure out what's not working properly.

    Continue reading...

Compartilhe esta Página