laravel phpunit test 返回 302 进行错误验证,为什么不是 422

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35025347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:06:55  来源:igfitidea点击:

phpunit test returns 302 for bad validation, why not 422

phplaravelphpunitlaravel-5.2

提问by cre8

I have a request class that fails for a post request. When I call it with ajax I get an 422 because the validation rules failed. But when I use phpunit for test for the same route with same values, it returns a 302.

我有一个请求类失败的 post 请求。当我用 ajax 调用它时,我得到一个 422,因为验证规则失败。但是当我使用 phpunit 测试具有相同值的相同路由时,它返回 302。

I also get no error messages like "field foobar is required" just the 302.

我也没有收到错误消息,例如“需要字段 foobar”,只是 302。

So how can I get the error messages to check if they are equals or not?

那么如何获取错误消息以检查它们是否相等?

Here is my testcode:

这是我的测试代码:

//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

回答by patricus

When the validation on the FormRequestfails, it checks to see if the request was ajax or if it accepts a json response. If so, it will return a json response with the 422 status code. If not, it will return a redirect to a specified url (previous, by default). So, in order to get the response on failure you're looking for (422), you need to make a json request or an ajax request.

当验证FormRequest失败时,它会检查请求是 ajax 还是接受 json 响应。如果是这样,它将返回一个带有 422 状态代码的 json 响应。如果没有,它将返回一个重定向到指定的 url(以前的,默认情况下)。因此,为了获得您正在寻找的失败响应(422),您需要发出 json 请求或 ajax 请求。

JSON

JSON

To make a json request, you should use the json()method:

要发出 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);

There are also getJson(), postJson(), putJson(), patchJson(), and deleteJson()shortcut methods if you think that looks cleaner than passing the method as a parameter.

还有getJson(), postJson(), putJson(), patchJson(), 和deleteJson()快捷方法,如果您认为这比将方法作为参数传递更简洁的话。

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

AJAX

AJAX

To make an ajax request, you need to add in the ajax headers. For this, you can continue to use the post()method:

要发出 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);

回答by azurecorn

For Laravel 6 this works:

对于 Laravel 6,这有效:

withHeaders(['Accept' => 'application/json'])

For an example:

例如:

 $this->withHeaders(['Accept' => 'application/json'])
     ->post(route('user.register'), $data)
     ->assertStatus(422)
     ->assertJson($expectedResponse);

If it's needed for multiple test classes, it can be placed in tests/TestCase.phpand it will be set up for all test cases.

如果多个测试类需要它,它可以放在里面tests/TestCase.php,它将为所有测试用例设置。

For an example:

例如:

public function setup(): void
{
    $this->withHeaders([
        'Accept' => 'application/json',
        'X-Requested-With' => 'XMLHttpRequest'
    ]);
}

Those headers set in tests/TestCase.phpcan be extended at any point by the same way. For an example:

tests/TestCase.php可以通过相同的方式在任何时候扩展这些设置的标头。例如:

$this->withHeaders([
    'Authorization' => 'Bearer '.$responseArray['access_token']
]);