4

Is there a way to make a setUp() and tearDown() method that tell laravel something like "make all of this insertions in Database, but when you finish all the tests, delete all insertions without reset the database"

I need to do tests like this:

/** @test */
public function testRegistration(){

   $account = [
    'name'     => 'test',
    'email'    => 'test@gmail.com',
    'password' => '123451234'
    ];

    $this->visit('/register')
         ->type($account['name'],'name')
         ->type($account['email'],'email')
         ->type($account['password'],'password')
         ->type($account['password'],'password_confirmation')
         ->press('Register')
         ->seePageIs('/home');
}

At first, i can run phpunit and it will work, if i run again, of course it returns an error telling me that i cannot use this email because already in the database.

The problem is that i can't simple reset my database, i have already 12.000 rows inserted in my database and i can't create a test_database because i need these 12.000 rows inserted to my application makes sense.

I haven't found any information that i can use, all that i can find is "make your test call migration::refresh and send 4 minutes to populate your table again", but i'm sure that is possible to find a better solution!

Also, where do i put my setUp() method and where do i call it?

Thank you.

Filipe Filardi
  • 170
  • 2
  • 12

1 Answers1

1

See this and this documentation (working with transaction).

This trait will only wrap the default database connection in a transaction.

In other words, the insertion will be "faked" so you will not need to remove it everytime.

Another option is to add another test in which you delete every insertion you have made.

User::where('name', 'test')->where('email','test@gmail.com')->delete();
Wistar
  • 3,770
  • 4
  • 45
  • 70