Validation Error on Laravel 5.1

Hey. I am trying to create a test for a laravel controller that is not view dependent. I have the following method in the controller class:

public function store(Request $request)
{
    $this->validate($request,[
            'name'=>'required',
            'login'=>'required',
            'password'=>'required'
        ]);
    //TODO: Store to database
    return redirect('usuario/nuevo');
}

And I found the following code to check if the request had any errors:

public function testStore()
{
    $response=$this->call('GET','usuario.store');
    $this->assertSessionHasErrors();
}

As this test should pass, as I send the request without filling in the field, however, PhpUnit returns the following message:

Session missing key: errors
Failed asserting that false is true.

The only way I can get it to work is to try to "see" the error message on the response page by running the test as follows:

public function testStore()
{
    $this->visit('/usuario/nuevo')
        ->press('Crear')
        ->see('Whoops');
}

: 1) "" ( ) 2) "", , , .

, , , .

+4
2

       $this->startSession();
       $this->call('GET','usuario.store', array(
        'name' => '',
        'login' => '',
        'password' => ''
    ));
    $errors = session('errors');
    $this->assertSessionHasErrors();
    $this->assertEquals($errors->get('name')[0],""your custom error message);// only for checking exact message
+2

, , , , .

(), POST, GET, .

0

Source: https://habr.com/ru/post/1655239/


All Articles