Laravel黄昏:使用Dropzone.js上传测试文件

Laravel黄昏:使用Dropzone.js上传测试文件,laravel,testing,laravel-dusk,Laravel,Testing,Laravel Dusk,我正在使用laravel5.6和Dask进行这个特定的测试 我试图在我的dropzone中声明文件上载。但是我的Dropzone是以一种没有文件输入元素的方式创建的。所以我不能使用attach()方法 所以我尝试了以下方法 $file = new \Symfony\Component\HttpFoundation\File\UploadedFile(base_path() . '/tests/samples/Cylinder.stl', 'Cylinder.stl'); $response =

我正在使用laravel
5.6
Dask
进行这个特定的测试

我试图在我的dropzone中声明文件上载。但是我的Dropzone是以一种没有
文件
输入元素的方式创建的。所以我不能使用
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隐藏输入”类的输入字段。 您可以在html页面的底部找到它,可能就在
标记之前:

因此,您可以通过“附加”方法告诉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');

正如在另一个答案中提到的,该方法存在于dropzone.js创建的虚拟输入字段中。但是,如果您有多个单独的DropZone,这不是一个可行的解决方案,因为无法判断哪个输入字段是要附加文件的正确字段

最好的解决方案在于为dropzone提供最佳的解决方案。指定可用于区分dropzone的值,例如包含正在配置的dropzone的div

然后,可以将文件附加到它(使用Faker作为生成文件的快捷方式):


你能给我举个简单的例子吗?@EugenevanderMerwe我根据你的评论验证了这个答案,因为我现在没有环境来测试它。但答案似乎合乎逻辑且正确。
$image = $faker->image('/tmp', 640, 480);
$browser->attach('#dropzone-image1 input.dz-hidden-input', $image);
$browser->attach('#dropzone-image2 input.dz-hidden-input', $image);