代码之家  ›  专栏  ›  技术社区  ›  Atnaize

Laravel dask:用Dropzone.js上传测试文件

  •  2
  • Atnaize  · 技术社区  · 7 年前

    我在用拉维 5.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"                                      
        }                                                          
      ]                                                            
    }      
    

    当然,这是工作时,我做手动。

    2 回复  |  直到 6 年前
        1
  •  3
  •   maelga    6 年前

    Dropzonejs添加一个输入字段 dz隐藏输入 “班级。 您可以在html页面的底部找到它,可能就在 </body> 标签:

    <input type="file" multiple="multiple" class="dz-hidden-input">

    所以您可以通过attach方法告诉dask匹配那个确切的选择器:

    $browser->attach('input.dz-hidden-input', storage_path('app/public/testing/test-file.jpg'));

    如果您有一个显示文件名的dropzone预览和一个“Remove file”按钮,那么您可以链接一些这样的断言,以确保文件也可以被删除:

    $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');
    
        2
  •  1
  •   Connor Cook    6 年前

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

    最好的解决办法是 hiddenInputContainer configuration value 对于dropzone。指定一个可用于区分dropzone的值,例如包含正在配置的dropzone的div。

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

    $image = $faker->image('/tmp', 640, 480);
    $browser->attach('#dropzone-image1 input.dz-hidden-input', $image);
    $browser->attach('#dropzone-image2 input.dz-hidden-input', $image);
    
    推荐文章