samedi 30 juillet 2016

Wrong validator redirect with laravel (phpunit)

I tried to test a form (post) with phpunit and Laravel.

I use the validator to check that my two inputs are filled. On graphical use my code totally works. When an input was not filled i'm redirected on form page with errors and old inputs informations.

But when i try to launch my unit tests with phpunit the validator does not redirect on the URL of the form when the validation fail, I'm redirect on an other page completely beside the point.

For information, I reach the form page. The problem become after sending the form with post method.

This is my routes :

Route::group(['prefix' => 'users', 'middleware' => ['web', 'auth']], function() {

Route::get('/', 'DashboardUsersController@users');
Route::get('/create', 'DashboardUsersController@usersCreateView');

Route::post('/create', 'DashboardUsersController@userCreate');

The controller :

public function userCreate(Request $request){
    $data = Input::all();

    $this->validate($request, [
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users'
    ]);

    $password = User::random_password(12);
    $data['password'] = $password;
    Mail::send('auth.emails.register', $data, function($message) use ($data){
        $message->from('donotreply@****.fr', 'Test');
        $message->to($data['email']);
    });

    User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($password),
    ]);
    Session::flash('success', "L'utilisateur a été ajouté avec succès.");
    return redirect('/users');
}

My simple unit test :

public function testAddUser(){
    $user = factory(App\User::class)->create();

    //Verify that empty form redirect with errors
    $resp = $this->actingAs($user)
        ->visit('/users/create')
        ->click('Ajouter')
        ->see(trans('validation.required', ['attribute' => 'Nom']));

     //More when this validate...
}

And to finish the error message gave by phpunit on account of the wrong redirection and see() assertion :

Failed asserting that the page contains the HTML [Le champ Nom est obligatoire.]. Please check the content above.

Anyone got an idea of the problem ?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2akI0JP
via IFTTT

Aucun commentaire:

Enregistrer un commentaire