0
0
Laravelframework~5 mins

Database testing (RefreshDatabase) in Laravel - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the RefreshDatabase trait in Laravel testing?
The RefreshDatabase trait resets the database before each test by running migrations. This ensures tests start with a clean database state, avoiding data conflicts.
Click to reveal answer
intermediate
How does RefreshDatabase differ from DatabaseTransactions in Laravel tests?
RefreshDatabase rebuilds the database schema before each test, while DatabaseTransactions wraps tests in a transaction that rolls back after the test. RefreshDatabase is better for tests that modify schema or need a fresh start.
Click to reveal answer
beginner
When should you use RefreshDatabase in your Laravel tests?
Use RefreshDatabase when your tests change the database schema or when you want to ensure no leftover data affects your tests. It is ideal for integration tests involving database state.
Click to reveal answer
intermediate
What happens behind the scenes when you use RefreshDatabase in a Laravel test?
Laravel runs the database migrations to rebuild the schema from scratch before each test. This wipes all data and resets tables to their initial state.
Click to reveal answer
intermediate
Can RefreshDatabase be used with in-memory SQLite databases? Why or why not?
Yes, RefreshDatabase works well with in-memory SQLite databases because migrations run quickly and the database is fresh for each test run, making tests fast and isolated.
Click to reveal answer
What does the RefreshDatabase trait do in Laravel tests?
ACaches database queries for faster tests
BRolls back database transactions after each test
CResets the database by running migrations before each test
DSeeds the database with test data automatically
Which trait is better for tests that only need to rollback data changes without rebuilding schema?
AWithFaker
BRefreshDatabase
CWithoutMiddleware
DDatabaseTransactions
Why might RefreshDatabase slow down tests compared to DatabaseTransactions?
ABecause it runs migrations before each test
BBecause it caches queries
CBecause it disables middleware
DBecause it seeds the database multiple times
Can RefreshDatabase be used with an in-memory SQLite database?
AYes, it works well and is fast
BNo, it only works with MySQL
CNo, it requires a persistent database
DYes, but it disables migrations
What is a key benefit of using RefreshDatabase in tests?
ADisables authentication middleware
BEnsures tests run with a clean database state
CAutomatically mocks external APIs
DImproves query performance caching
Explain how the RefreshDatabase trait helps maintain test reliability in Laravel.
Think about how tests can fail if old data remains.
You got /4 concepts.
    Compare RefreshDatabase and DatabaseTransactions traits and when to use each.
    Consider schema changes versus data changes.
    You got /4 concepts.