0
0
Laravelframework~10 mins

Feature tests 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 create a basic feature test class in Laravel.

Laravel
<?php

namespace Tests\Feature;

use Tests\TestCase;

class ExampleTest extends TestCase
{
    public function test_basic_example()
    {
        $response = $this->[1]('/');

        $response->assertStatus(200);
    }
}
Drag options to blanks, or click blank then click option'
Aget
Bpost
Cput
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST instead of GET for a simple page request.
Confusing HTTP verbs in the test method.
2fill in blank
medium

Complete the code to assert that the response contains the text 'Welcome'.

Laravel
$response = $this->get('/home');
$response->[1]('Welcome');
Drag options to blanks, or click blank then click option'
AassertRedirect
BassertJson
CassertSeeText
DassertStatus
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertJson when the response is HTML.
Using assertRedirect instead of checking content.
3fill in blank
hard

Fix the error in the test method to correctly test a POST request with data.

Laravel
public function test_submit_form()
{
    $response = $this->post('/submit', [1]);
    $response->assertStatus(302);
}
Drag options to blanks, or click blank then click option'
A['John', 'john@example.com']
B'name=John&email=john@example.com'
Cnull
D['name' => 'John', 'email' => 'john@example.com']
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a query string instead of an array.
Passing null or a list without keys.
4fill in blank
hard

Fill both blanks to test that a user is authenticated and redirected after login.

Laravel
public function test_user_login()
{
    $user = User::factory()->create();

    $response = $this->post('/login', ['email' => $user->email, 'password' => 'password']);

    $response->assertRedirect([1]);
    $this->assertAuthenticatedAs([2]);
}
Drag options to blanks, or click blank then click option'
A'/dashboard'
B$user
C'/home'
Dauth()->user()
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong redirect URL.
Checking authentication against a wrong user variable.
5fill in blank
hard

Fill all three blanks to create a feature test that checks JSON response structure and status.

Laravel
public function test_api_response()
{
    $response = $this->json([1], '/api/data');

    $response->assertStatus([2])
             ->assertJsonStructure([3]);
}
Drag options to blanks, or click blank then click option'
A'GET'
B200
C['data', 'meta']
D'POST'
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST instead of GET for fetching data.
Expecting wrong status code.
Checking incorrect JSON keys.