jeudi 17 mai 2018

Laravel PHP Unit test. Smiliar tests but one fails

These 2 tests are for the password reset. The first test is failing. The second test is passing. They are very similar. I don't understand why the first test is failing.

In the first test:

  1. I'm creating a user.
  2. Getting a password reset token
  3. Making a post to change my password with the correct data.
  4. It keeps telling my I have an error with my email. I don't understand why.

    <?php
    
    namespace Tests\Controllers\Unit;
    
    use Tests\TestCase;
    use Illuminate\Support\Facades\DB;
    use Illuminate\Support\Facades\Hash;
    use Illuminate\Foundation\Testing\RefreshDatabase;
    
    class ResetPasswordTest extends TestCase
    {
    
        use RefreshDatabase;
    
    
        /** @test */
        public function failing_does_change_a_users_password()
        {
            // Create user 
            $user = factory('App\User')->create();
    
            // This post route creates a new password reset token and stores it into the database.
            $this->post(route('password.email'), ['email' => $user->email]);
    
            // I get the token
            $token = DB::table('password_resets')->whereEmail($user->email)->first()->token;
    
            $this->from(route('password.reset', $token));
    
            // Change password with the correct data
            $response = $this->post('password/reset', [
                'token'                 => $token,
                'email'                 => $user->email,
                'password'              => 'password',
                'password_confirmation' => 'password'
            ]);
    
            // This is true... It tells me I have an error with my email.
            $reponse->assertSessionHasErrors('email');
    
            // This is false. The password is not changed to the new password.
            $this->assertTrue(Hash::check('password', $user->fresh()->password));
        }
    
    
        // This test is the working one.
    
        /** @test */
        public function passing_does_change_a_users_password()
        {
            $user = factory('App\User')->create();
    
            $token = Password::createToken($user);
            $new_password = 'bestpassword99';
    
            DB::table('password_resets')->insert([
                'token'   => $token,
                'email'   => $user->email,
            ]);
    
            $this->from(route('password.reset', $token));
    
            $response = $this->post('password/reset', [
                'token'                 => $token,
                'email'                 => $user->email,
                'password'              => $new_password,
                'password_confirmation' => $new_password
            ]);
    
    
            $this->assertTrue(Hash::check($new_password, $user->fresh()->password));
        }
    
    }  
    
    


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire