laravel - Laravel 黄昏:使用 Dropzone.js 上传测试文件
问题描述
我正在使用 laravel5.6
和Dusk
这个特定的测试。
我正在尝试在我的 dropzone 中声明文件上传。但是我的 Dropzone 是以我没有file
输入元素的方式创建的。所以我不能使用该attach()
方法。
所以我尝试了以下
$file = new \Symfony\Component\HttpFoundation\File\UploadedFile(base_path() . '/tests/samples/Cylinder.stl', 'Cylinder.stl');
$response = $this->actingAs( $this->user )
->from( 'my-url' )
->post( route('attachments.store' ) , [
'file' => $file
]);
但是错误包中包含此错误
"errors" => Illuminate\Support\ViewErrorBag {#1194
#bags: array:1 [
"default" => Illuminate\Support\MessageBag {#1189
#messages: array:1 [
"file" => array:1 [
0 => "The file failed to upload."
]
]
#format: ":message"
}
]
}
当然,当我手动执行此操作时,这是可行的。
解决方案
Dropzonejs 添加一个带有特定 ' dz-hidden-input ' 类的输入字段。您可以在 html 页面的底部找到它,可能就在</body>
标签之前:
<input type="file" multiple="multiple" class="dz-hidden-input">
所以你可以通过 attach 方法告诉 Dusk 匹配那个精确的选择器:
$browser->attach('input.dz-hidden-input', storage_path('app/public/testing/test-file.jpg'));
如果你有一个显示文件名和“删除文件”按钮的 dropzone 预览,你可以链接一些这样的断言,以确保文件也可以被删除:
$browser->attach('input.dz-hidden-input', storage_path('app/public/testing/test-file.jpg'))
->assertSee('test-file.jpg')
->assertSeeLink('Remove file')
->clickLink('Remove file')
->assertDontSee('test-file.jpg');