Laravel 黄昏 : test file upload with Dropzone. js

标签 laravel testing laravel-dusk

我正在使用 laravel 5.6Dusk 进行此特定测试。

我正在尝试在我的放置区中断言文件上传。但是我的 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/

相关文章:

r - R 中的三向卡方检验

php - Laravel Dusk 和内存使用 - 如何关闭浏览器和自定义进程

Laravel 迁移 - 更新枚举选项

php - ERR_INCOMPLETE_CHUNKED_ENCODING 在全新的 Laravel 安装上

javascript - 如果当前测试失败,如何检查mocha(tdd)的拆解方法?

php - Laravel Dusk - 类配置不存在

laravel - 在 laravel dusk 中按下 enter 键盘

laravel - 混合内容 : The page at 'domain' was loaded over HTTPS, 但请求了不安全的 XMLHttpRequest 端点

php - 没有命名空间时如何在 laravel 5.6 上使用 google-api-php-client

unit-testing - 用于测试的库是否包含在最终构建中