Phpunit test returns 302 for a bad check, why not 422

I have a request class that is not suitable for a mail request. When I call it using ajax, I get 422 because the validation rules failed. But when I use phpunit to test for the same route with the same values, it returns 302.

I also do not receive error messages such as "foobar field required" only 302.

So, how can I get error messages to check if they are equal or not?

Here is my test code:

//post exam
$this->post('modul/foo/exam', [
    'date' => '2016-01-01'
])
    ->assertResponseStatus(200);

//post exam again
$this->post('modul/foo/exam', [
    'date' => '2016-01-01'
])
    ->assertResponseStatus(302); //need to get 422 with th errors because its an api
+4
source share
1 answer

FormRequest , , ajax json. , json 422. , URL- (, ). , , (422), json ajax.

JSON

json, json():

//post exam
$this->json('POST', 'modul/foo/exam', [
        'date' => '2016-01-01'
    ])
    ->assertResponseStatus(200);

//post exam again
$this->json('POST', 'modul/foo/exam', [
        'date' => 'some invalid date'
    ])
    ->assertResponseStatus(422);

AJAX

ajax, ajax. post():

//post exam
$this->post(, 'modul/foo/exam', [
        'date' => '2016-01-01'
    ], ['HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'])
    ->assertResponseStatus(200);

//post exam again
$this->post('modul/foo/exam', [
        'date' => 'some invalid date'
    ], ['HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'])
    ->assertResponseStatus(422);
+5

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


All Articles