zaster's avatar

Test Function Names

I am just experimenting with laravel testing

Just curious whether is there any better way to name the below mentioned function names

I know that these are not the best tests, but i am just testing.

    public function test_what_a_guest_can_see_when_login_url_is_visited()
    {
        $response = $this->get('/login');

        $response->assertSee('Home');
        $response->assertSee('Log in');
        $response->assertSee('Register');

        $response->assertStatus(200);

    }

    public function test_what_a_guest_can_see_when_register_url_is_visited()
    {
        $response = $this->get('/register');

        $response->assertSee('Home');
        $response->assertSee('Log in');
        $response->assertSee('Register');

        $response->assertStatus(200);

    }
0 likes
5 replies
lemmon's avatar

@zaster

I know that these are not the best tests, but i am just testing.

I have not seen a test for this.

Name for readability ie

    public function test_a_guest_can_visit_login_page()
    {
        $this->get('/login')
            ->assertSee('Log in')
            ->assertSee('Register')
            ->assertSee('Home')
            ->assertStatus(200);
    }

The assertions tell the rest of the story.

Edit

In this case some of the function name can be inferred through the body of the test.

1 like
automica's avatar
automica
Best Answer
Level 54

@zaster if you use the @test annotation, you don't need to use test in your test name, and can do something like the following

    /**
     * @test
     *
     * @return void
     */
    public function can_access_welcome_page(): void
    {
        $response = $this->get(route('home'));

        $response->assertSuccessful(); // same as assertStatus(200) but nicer as it doesn't have the magic number
    }

i wouldn't worry about adding guest to your test name. Assume everyone is a guest, and then if they have more privileges then describe those.

eg

can_access_login_page()

authorised_user_can_access_dashboard()

1 like
zaster's avatar

@automica

Tests does work without : void

E.g.

/**
     * @test
     *
     * @return void
     */
    public function a_guest_can_access_welcome_page()
    {
        $response = $this->get('/');

        $response->assertSee('Log in');
        $response->assertSee('Register');

        $response->assertSuccessful();
    }

Is it ok to proceed like that ?

From Where did you find the assertSuccessful() method ? What should i use when it is a different assert status such as

$response->assertStatus(403);

Please or to participate in this conversation.