0
0
Laravelframework~10 mins

Database testing (RefreshDatabase) in Laravel - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to use the RefreshDatabase trait in a Laravel test class.

Laravel
<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\[1];
use Tests\TestCase;

class UserTest extends TestCase
{
    use RefreshDatabase;

    public function test_example()
    {
        $this->assertTrue(true);
    }
}
Drag options to blanks, or click blank then click option'
ADatabaseMigrations
BWithoutMiddleware
CWithFaker
DRefreshDatabase
Attempts:
3 left
💡 Hint
Common Mistakes
Using DatabaseMigrations instead of RefreshDatabase
Forgetting to import the trait
Not using any trait to reset the database
2fill in blank
medium

Complete the code to run a test that checks if a user record exists after creation.

Laravel
public function test_user_creation()
{
    \App\Models\User::factory()->create();

    $this->assertDatabaseHas('[1]', [
        'email' => 'test@example.com',
    ]);
}
Drag options to blanks, or click blank then click option'
Ausers
Buser
Caccounts
Dprofiles
Attempts:
3 left
💡 Hint
Common Mistakes
Using singular 'user' instead of 'users'
Using unrelated table names like 'accounts' or 'profiles'
3fill in blank
hard

Fix the error in the test method to correctly refresh the database before running.

Laravel
public function test_database_refresh()
{
    $this->[1]();
    $this->assertTrue(true);
}
Drag options to blanks, or click blank then click option'
AmigrateRefresh
BrefreshDatabase
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like migrateRefresh
Calling refreshDatabase manually instead of using the trait
4fill in blank
hard

Fill both blanks to correctly import and use the RefreshDatabase trait in a Laravel test.

Laravel
<?php

namespace Tests\Unit;

use Illuminate\Foundation\Testing\[1];
use Tests\TestCase;

class ProductTest extends TestCase
{
    use [2];

    public function test_product_creation()
    {
        $this->assertTrue(true);
    }
}
Drag options to blanks, or click blank then click option'
ARefreshDatabase
BDatabaseMigrations
DWithFaker
Attempts:
3 left
💡 Hint
Common Mistakes
Importing one trait but using another
Forgetting to use the trait inside the class
5fill in blank
hard

Fill all three blanks to write a test that creates a user and asserts the database has the user email.

Laravel
public function test_user_database_entry()
{
    $user = \App\Models\User::factory()->create();

    $this->assertDatabaseHas([1], [
        [2] => [3],
    ]);
}
Drag options to blanks, or click blank then click option'
A'users'
B'email'
C'test@example.com'
D'name'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong table name or missing quotes
Using wrong column name
Using a value that does not match the created user