gpt4 book ai didi

laravel - 如何测试将虚假文件上传到验证 Laravel 文件的路由?

转载 作者:行者123 更新时间:2023-11-28 21:17:43 24 4
gpt4 key购买 nike

我试图在我的 Controller 中为路由“存储”编写一个测试单元,通过我的方法的验证器传递一个要测试的假文件,但我得到的只是数据不是文件:

Illuminate\Foundation\Testing\TestResponse {
+baseResponse: Illuminate\Http\JsonResponse {
#data: "{
"message":"The given data was invalid.",
"errors":{"invoice":["The invoice must be a file."]}
}"

代码:

  • 测试:
        $data = factory('App\Domain\X\X')->raw(['creator_id' => $user->id]);
$data['invoice'] = UploadedFile::fake()->create('invoice.xlsx');
$response = $this->json('POST', route('x.store', $data));
  • Controller :
 public function store(XXXRequest $request)
{
...
  • 请求:
class XXXRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required',
'invoice' => 'nullable|file',
];
}

最佳答案

试试这个:

测试:

    $data = factory('App\Domain\X\X')->raw(['creator_id' => $user->id]);
$data['invoice'] = UploadedFile::fake()->create('invoice.xlsx');
$response = $this->json('POST', route('x.store', $data), [
'name' => 'abc',
'invoice' => $data['invoice']
]);

关于laravel - 如何测试将虚假文件上传到验证 Laravel 文件的路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55456022/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com