Laravel黄昏:使用Dropzone.js上传测试文件 [英] Laravel dusk: test file upload with Dropzone.js
问题描述
此测试使用的是laravel 5.6
和 Dusk
.
I'm using laravel 5.6
and Dusk
for this specific test.
我正在尝试在我的Dropzone中声明文件上传.但是我的Dropzone的创建方式没有 file
输入元素.所以我不能使用 attach()
方法.
I'm trying to assert a file upload in my dropzone. But my Dropzone is created in a way that I don't have a file
input element. So I can't use the attach()
method.
所以我尝试了以下
$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
]);
但是错误包中包含此错误
But the error bag is containing this error
"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"
}
]
}
当然,当我手动执行此操作时,它会起作用.
And of course this is working when I do it manually.
推荐答案
Dropzonejs添加具有特定' dz-hidden-input '类的输入字段.您可以在html页面的底部找到它,很可能在</body>
标记之前:
Dropzonejs adds an input field with a specific 'dz-hidden-input' class.
You can find it at the bottom of your html page, likely just before the </body>
tag:
< input type ="file" multiple ="multiple" class ="dz-hidden-input">
因此您可以通过attach方法告诉Dusk匹配该确切的选择器:
So you can tell Dusk to match that exact selector through the attach method:
$ 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'));
如果您有一个显示文件名的dropzone预览和一个删除文件"按钮,则可以像这样链接一些断言,以确保也可以删除该文件:
If you have a dropzone preview showing the file name and a 'Remove file' button, you can then chain some assertions like this to make sure the file can also be removed:
$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黄昏:使用Dropzone.js上传测试文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!