BrownieCoffee's avatar

Test redirection signup to login

Hi there !

I'm doing test for signup right now and I have problem. I try to redirect user to route('login') from route('signup').

My validation request

public function signup()
    {
        return view('auth.signup');
    }

    public function signupPost(Request $request)
    {


        $requestTwo = $request->validate([
            'username' => 'required|string|min:3|unique:users',
            'email' => 'required|string|email|unique:users',
            'password' => 'required|min:6|confirmed'
        ]);


        $user = new User();
        $user->username = $request->username;
        $user->email = $request->email;
        $user->password = $request->password;
        $user->save();


            return redirect()->route('login');
    }

my signup Test

public function guest_can_register()
    {
        $this->expectException(AuthenticationException::class);

        $this->withoutMiddleware();

        // $this->withoutExceptionHandling();


        $user = factory(User::class)->create(
            ['username' => 'azerty']
        );

        $userRequests = [
            'username' => $user->username,
            'email' => $user->email,
            'password' => $user->password,
            'password_confirmation' => $user->password
        ];

        $response = $this->from(route('signup'))->post(
            route('signupPost'),$userRequests
        );

        $response->assertRedirect(route('login'))
                    ->assertSessionHasNoErrors();

        $this->assertDatabaseHas('users', [
            'username' => $user->username,
            'email' => $user->email,
            'password' => $user->password
        ]);

    }

and this is the output of testing

 r guest can load signup page → Test code or tested code did not (only) close its own output buffers
  ✕ guest can register

  Tests:  1 failed, 1 risked, 1 passed, 2 pending

  Failed asserting that two strings are equal.

  at tests/Feature/SignupTest.php:67
    63|         $response = $this->from(route('signup'))->post(
    64|             route('signupPost'),$userRequests
    65|         );
    66| 
  > 67|         $response->assertRedirect(route('login'))
    68|                     ->assertSessionHasNoErrors();
    69| 
    70|         $this->assertDatabaseHas('users', [
    71|             'username' => $user->username,

--- Expected
+++ Actual
@@ @@
-'http://localhost/connexion'
+'http://localhost/inscription'


When I completed inputs manually on the webpage, It's works but not with my test and I don't know what's the problem...

Can you help me please? Thank you by advance :) See you.

0 likes
12 replies
bobbybouwmann's avatar

This could be related to caching, can you try php artisan route:clear

Another problem could be in the routes file itself. Can you show us your routes/web.php for the login and signup route?

BrownieCoffee's avatar

@bobbybouwmann Hi,

I just tryed php artisan route:clear it doesn't fix the problem... :/

Yes sure !

This is my web.php bellow.

Route::get('/', 'HomeController@index')->name('home');


/**AUTHENTIFICATION**/
/*CONNEXION*/
Route::get('/connexion', 'AuthentificationController@login')->name('login');

Route::post('/connexion', 'AuthentificationController@loginPost')->name('loginPost');


/*INSCRIPTION*/
Route::get('/inscription', 'AuthentificationController@signup')->name('signup');

Route::post('/inscription', 'AuthentificationController@signupPost')->name('signupPost');

bobbybouwmann's avatar

So it appears it redirects you back to the signup page. The only thing I can think of is that you have validation errors here.

Maybe the password is empty in your test. Just dump the $userRequests to see what value that holds.

BrownieCoffee's avatar

This is the output after dd($userRequests);

  RUNS  Tests\Feature\SignupTest
  • guest can register

  Tests:  1 risked, 1 passed, 3 pending
array:4 [
  "username" => "azerty"
  "email" => "[email protected]"
  "password" => "password12345"
  "password_confirmation" => "password12345"
]


bobbybouwmann's avatar

Is the username and email unique in the database? If not it could give you the validation error and therefore it doesn't work.

When running your tests make sure you always have a clean database. You can use the RefreshDatabase trait for that.

BrownieCoffee's avatar

@bobbybouwmann,

Yes, username and email are unique. I use 'RefreshDatabase' trait too. I really don't know what is the problem ...

bobbybouwmann's avatar

Mmh, I don't see it either other than you being redirected to the previous page...

BrownieCoffee's avatar

@bobbybouwmann I find I have something but I'm not sure about ->from() help using.

I try another way for my code.

   $response = $this->from('/connexion')->post('/inscription', $userRequests);

        $response->assertRedirect('/connexion');

        $this->assertDatabaseHas('users', [
            'username' => $user->username,
            'email' => $user->email,
            'password' => $user->password
        ]);

But I don't know why my test is correct now.

I tryed this route too inside from() helper

   $response = $this->from('/zano')->post('/inscription', $userRequests);

        $response->assertRedirect('/connexion');

        $this->assertDatabaseHas('users', [
            'username' => $user->username,
            'email' => $user->email,
            'password' => $user->password
        ]);

this is the output after run test


  Tests:  1 failed, 1 risked, 1 passed, 2 pending

  Failed asserting that two strings are equal.

  at tests/Feature/SignupTest.php:62
    58| 
    59|         // $response->post('/inscription', $userRequests);
    60|         // $response = $this->from('/connexion')->post('/inscription', $userRequests);
    61| 
  > 62|         $response->assertRedirect('/connexion');
    63| 
    64|         $this->assertDatabaseHas('users', [
    65|             'username' => $user->username,
    66|             'email' => $user->email,

--- Expected
+++ Actual
@@ @@
-'http://localhost/connexion'
+'http://localhost/zano'

So If I understood, it is necessary to add the target redirection page inside the from('') helper ?

bobbybouwmann's avatar

Why not just remove from and simply just post to /inscription?

To be honest, I never use from in my tests

bobbybouwmann's avatar

@browniecoffee Looking at your code again. It seems it redirects you back to /zano again, while your controller doesn't do that. The only thing I can think of is that the validation still fails...

Please or to participate in this conversation.