mcbates's avatar

How to get concise feedback on what went wrong in a http request based test?

Say I have the following test to test whether login works

    /** @test */
    public function a_user_giving_correct_login_data_can_log_in()
    {
        $user = factory(User::class)->make();

        $options = [
            'email' => $user->email,
            'password' => $user->password,
        ];

        $response = $this->post('/login', $options);
        $response->assertRedirect('/home');

    }

This does not work and I want to understand why.

How can I get concise information and what went wrong?

I usually add dd($response) after the post.

  • The result is the long long response in my terminal that I have to sift through to find the problem (in this case the validation exception)
  • I might add a grep filter to get just the context around the exceptions: | grep Exception -B 4 -A 4

I bet there are better ways to get feedback on what went wrong. I would be glad if you could share these!

0 likes
3 replies
m7vm7v's avatar

You are not creating the user - you are just 'make'-ing it. That does not store it in the database.

It fails because the password is Hashed.

when you make the $user the $user->password is NOT 'secret' but something like '$2y$10$snEIwInCoiIDlNmgmMnK'

So after you send this information the login will fail as its not the password that you are checking but the hashed version of it.

You could do -

    /** @test */
    public function a_user_giving_correct_login_data_can_log_in()
    {
        $theSecretPassword = 'secret';

        $user = factory(User::class)->create(['name' => 'Name', 'password' => Hash::make($theSecretPassword)]);

        $options = [
            'email' => $user->email,
            'password' => $theSecretPassword,
        ];

        $response = $this->post('/login', $options);
        $response->assertRedirect('/home');
    }
1 like
mcbates's avatar

@m7vm7v Thanks a lot for helping me with the problem that caused my test to fail. This helped me for this very specific situation.

My question originally was a big broader:

How can I help myself better when I have problems like these where I just get a very very long response and have to manually search for the problem in the terminal log…

m7vm7v's avatar
m7vm7v
Best Answer
Level 51

I would probably dd() each step mate. For example the first one would be the dd($user); then in your example you would have seen that the user's password is hashed so I would start searching how to pass a known password and save it so I can use it later as in my example.

Then I would not dd($response); as its a big collection of information. I would see the specific error message or status code. Most of the IDE's would help you when you start typing then will return helpful methods that you would have more specific information.

For each situation is different and what I would advise you is to have a lot of practice and try being more specific with debugging. Try the https://laracasts.com/series/lets-build-a-forum-with-laravel that would help you a lot.

Please or to participate in this conversation.