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'    => '[email protected]',
    '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.

1 Answer 1

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','[email protected]')->delete();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, exactly what i need!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.