Testing with Laravel 5.5

0

I want to test the user login only with PHPUnit for several recommendations as Laravel Dusk becomes slow.

My problem is that I want to test the part of when the user enters data in the form and I do not know if that can be done

   $this->actingAs($user); //USUARIO CREADO ANTERIORMENTE

   //When
   $response = $this->get(route('home'));

   //Then
   $response->assertStatus(200)
   ->assertViewIs('home');

The check I can do is that a logged-in user can go to the home path, but not when filling in the forms.

    
asked by Juan Pablo B 05.03.2018 в 15:27
source

1 answer

1

Well, I think what you're looking for is the type function that simulates entering data in the input html fields.

For example: You have the following html code, where there are the input s of email and password and the Login button

    <form class="form-horizontal" role="form" method="POST" action="{{ url('login') }}">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <div class="form-group ">
        <label class="col-md-4 control-label">Email</label>
        <div class="col-md-6">
            {!! Form::text('email',null,['class'=>'form-control','placeholder'=>'Ej:email','required']) !!}
        </div>
    </div>
    <div class="form-group">
    <label class="col-md-4 control-label">Contrase&ntildea</label>
        <div class="col-md-6">
            {!! Form::password('password',['class'=>'form-control','placeholder'=>'Ingrese su contrase&ntilde;a',"required"]) !!}
        </div>
     </div>
     <div class="form-group">
         <div class="col-md-6 col-md-offset-4">
             <button type="submit" class="btn btn-primary">Login</button>
          </div>
      </div>
    </form>

You can do a test function in the following way.

public function testLogin()
{
    $this->visit('/login');//pagina de login
    $this->type('[email protected]', 'email');//email es el name del input.
    $this->type('contraseña', 'password');//password es el name del input.
    $this->press('Login');//Login es el contenido del button.
    $this->seePageIs('/home');// pagina de redireccion.
}

you execute phpunit in your console, within your project and ready.

For this function to run correctly, the password and the email must belong to a user in the database, in addition to the functionality of the login must be complete.

    
answered by 05.03.2018 в 16:06