Home > Mobile >  Laravel8 tdd: session is missing expected key errors
Laravel8 tdd: session is missing expected key errors

Time:10-14

I just want to test an easy input field but I get this error!

enter image description here

/** @test */
public function email_must_be_a_valid_email()
 {
   $response = $this->post('/api/contacts', array_merge($this->data(), ['email' => 'NOT AN EMAIL']));
   $response->assertSessionHasErrors('email');
 }


private function data()
{
  return [
          'name' => 'Test Name',
          'email' => '[email protected]',
          'birthday' => '05/14/1988',
          'company' => 'ABC String'
        ];
}

Thses are the Controller and the request rule. I hope u can help me with it.

class StoreController extends Controller
{
    public function store(ContactsRequest $request)
    {
        $data = $request->validated();
        Contact::create($data);
    }
}

public function rules()
    {
        return [
            'name' => 'required',
            'email' => 'required|email',
            'birthday' => 'required',
            'company' => 'required',
        ];
    }

I hope u can help me with it.

CodePudding user response:

If you are using an api route then you should use

$response->assertJsonValidationErrors(['email']);

or

$response->assertInvalid(['email']);

which works for both JSON and session errors.

enter image description here

  • Related