Summer Sale! All accounts are 50% off this week.

jacobcollinsdev's avatar

Laravel Feature Tests - Code Commenting Question

I was working on a feature test and found that all my tests were passing, even when they shouldn't have. Here is the code:

public function a_user_can_create_a_collection()
    {
        $this->withoutExceptionHandling();

        $attributes = [
            'name' => $this->faker->sentence,
            'description' => $this->faker->paragraph
        ];

        $this->post('/collections', $attributes)->assertRedirect('/collections');

        $this->assertDatabaseHas('collections', $attributes);

        $this->get('/collections')->assertSee($attributes['name']);
    }

I didn't have the post route set up yet, so this should have failed but repeatedly showed that the tests were passing. Only when I added a comment before the function did it begin to correctly flag up that the test had failed.

/** @test */
public function a_user_can_create_a_collection()
    {
        $this->withoutExceptionHandling();

        $attributes = [
            'name' => $this->faker->sentence,
            'description' => $this->faker->paragraph
        ];

        $this->post('/collections', $attributes)->assertRedirect('/collections');

        $this->assertDatabaseHas('collections', $attributes);

        $this->get('/collections')->assertSee($attributes['name']);
    }

Could someone explain why the comment is so crucial for the test to work correctly?

0 likes
2 replies
webrobert's avatar
Level 51

that comment lets phpunit know that its a test. because its not prefixed with test_ in the name

1 like

Please or to participate in this conversation.