laravel - 带有上传文件的Laravel单元测试无法获取文件
问题描述
我正在开发 Laravel 5.6。
我的单元测试代码:
public function testUpload()
{
Storage::fake('local');
$this
->post(route('upload', ['file' => UploadedFile::fake()->create('file.txt', 1024)]))
->assertSuccessful();
}
$request->file('file')
但总是在控制器中null
。
route('upload')
是正确的,但dd($request->file('file'))
始终是空数组。null
dd($request->file()
有人对这个问题有任何想法吗?
解决方案
您想在post
函数的第二个参数中传递参数。这就是你想要的:
public function testUpload()
{
Storage::fake('local');
$this
->post(route('upload'), ['file' => UploadedFile::fake()->create('file.txt', 1024)])
->assertSuccessful();
}
推荐阅读
- build - 我应该如何让 CMake 也为我的内核创建 PTX 文件
- python - 如何删除 Matplotlib 动画中的线条?
- visual-studio-code - 以编程方式将文本写入 VS Code 中的新选项卡
- promise - 无法在 Blazor 中收到承诺
- bash - 如何在 bash 中处理“找不到命令”
- python - 如何以编程方式关闭 matplotlib 窗口?
- vue.js - 如何在 Vue.js 中使用 codemirror 发送行号?
- javascript - 模块代码错误试图分离我的代码
- scala - 如何使用 Spark 中的 scala 将单个单元格中的数据提取到多个列中
- javascript - 使用 unshift() 添加数据时获得漂亮的 chart.js 动画?