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

BernardoBF4's avatar

Naming my tests

I am currently using TDD for writing a system and I noticed I am naming my tests with the same start: a_logged_user. Perceiving this gave me the idea to write a test for my middleware (the one that verifies if the user is logged to access certain routes) and removing the "a_logged_user" from the test that involve a route guarded by that middleware. This makes sense or is it better to write these full names (more extensive also) with "a_logged_user"?

0 likes
4 replies
BernardoBF4's avatar

@aosdev Reading those post has made me think I shouldn't keep the "a_logged_user" since it's as if my tests were testing authentication all the time too, while 1) it's not; and 2) it should only do once thing per test. Thank you a lot :)

1 like
tykus's avatar

I've bounced both ways on this; and for now mostly write a simple test at the beginning of a Test class to assert that the user should be authenticated (or not), e.g.

class CreatePostTest extends TestCase
{
    /** @test */
    public function user_must_be_authenticated()
    {
		$this->post(route('posts.store', $this->validParams()))
            ->assertUnauthorized();
    }

    // other test examples
}

It is quick and simple, clear and sets the authentication expectations for the rest of the Test class examples, which can be named for the feature they test rather than something like an_authenticated_user_can_do_the_action_ or an_authenticated_user_can_see_the_thing_

1 like
Tray2's avatar

One way you can go, is to name the test files like CreatePostGuestTest and CreatePostUserTest or something along those lines.

1 like

Please or to participate in this conversation.