0
0
Laravelframework~10 mins

Why testing ensures reliability in Laravel - Test Your Understanding

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

Complete the code to define a basic test method in Laravel.

Laravel
public function test_example() {
    $response = $this->[1]('/');
    $response->assertStatus(200);
}
Drag options to blanks, or click blank then click option'
Aget
Bcall
Cvisit
Dfetch
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'visit' which is not a Laravel test method.
Using 'fetch' which is not defined in Laravel tests.
2fill in blank
medium

Complete the code to assert that the response contains specific text.

Laravel
$response->assert[1]('Welcome');
Drag options to blanks, or click blank then click option'
ASeeText
BSeeInDatabase
CSee
DSeeTextContains
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'assertSeeText' which is not a Laravel method.
Using 'assertSeeInDatabase' which checks database, not response.
3fill in blank
hard

Fix the error in the test method to correctly check database records.

Laravel
$this->assertDatabaseHas('[1]', ['email' => 'user@example.com']);
Drag options to blanks, or click blank then click option'
Aaccounts
Busers
Cuser
Dusers_table
Attempts:
3 left
💡 Hint
Common Mistakes
Adding '_table' suffix which is incorrect.
Using singular 'user' instead of plural 'users'.
4fill in blank
hard

Fill both blanks to create a test that checks if a user is redirected after login.

Laravel
public function test_redirect_after_login() {
    $response = $this->post('/login', ['email' => 'user@example.com', 'password' => 'secret']);
    $response->assert[1]('/home');
    $this->assertAuthenticatedAs([2]);
}
Drag options to blanks, or click blank then click option'
AassertRedirect
BassertStatus
C$user
Dauth()->user()
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertStatus instead of assertRedirect for redirect check.
Passing a variable instead of auth()->user() for authenticated user.
5fill in blank
hard

Fill all three blanks to write a test that creates a user, logs in, and checks the dashboard view.

Laravel
public function test_dashboard_access() {
    $user = User::factory()->[1]();
    $this->actingAs($user);
    $response = $this->[2]('/dashboard');
    $response->assert[3]('Dashboard');
}
Drag options to blanks, or click blank then click option'
Acreate
Bget
CassertSee
Dmake
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'make' which does not save the user to the database.
Using 'post' instead of 'get' for dashboard access.
Using 'assertStatus' instead of 'assertSee' to check content.