我正在使用 laravel 5.6
和 Dusk
进行此特定测试。
我正在尝试在我的放置区中断言文件上传。但是我的 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">
所以你可以告诉 Dusk 通过 attach 方法匹配那个精确的选择器:
$browser->attach('input.dz-hidden-input', storage_path('app/public/testing/test-file.jpg'));
如果你有一个显示文件名的拖放区预览和一个“删除文件”按钮,你可以像这样链接一些断言以确保文件也可以被删除:
$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');
关于Laravel 黄昏 : test file upload with Dropzone. js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51763089/