简体   繁体   English

在Capybara中测试JQuery上传:文件没有附加

[英]Testing JQuery Upload in Capybara: Files are not attaching

I am currently building a rails app that uses the jquery-upload plugin to allows users to upload files to do things such as set their user avatar. 我目前正在构建一个使用jquery-upload插件的rails应用程序,允许用户上传文件来执行诸如设置用户头像之类的操作。 If the upload is successful, it sets a hidden field on the form with the upload ID, and when the form is submitted, the association is saved. 如果上传成功,则会在表单上使用上载ID设置隐藏字段,并在提交表单时保存关联。 When I manually test this, it works the way it is supposed to. 当我手动测试它时,它按照预期的方式工作。 However, I cannot get my RSpec tests to pass. 但是,我无法通过我的RSpec测试。

I am using RSpec as my testing framework and Capybara-webkit as my javascript drive. 我使用RSpec作为我的测试框架,使用Capybara-webkit作为我的javascript驱动器。 The field where the file is supposed to attach looks like this 文件应附加的字段如下所示

= file_field_tag :file, class: "upload_file_field" (Also, using slim for templating) = file_field_tag :file, class: "upload_file_field" (另外,使用slim来进行模板化)

The coffeescript that handles the file upload looks like this 处理文件上传的coffeescript看起来像这样

$element.fileupload
  dropZone: $dropzoneElement
  url: "/uploads.json"
  method: "PATCH"
  fail: (e, data) =>
    @showErrorOnFailure(e, data)
  done: (e, data) =>
    @onSuccessfulUpload(e, data)

Controller code that handles file uploads looks like this 处理文件上传的控制器代码如下所示

class UploadsController < ApplicationController
  def create
    @upload = Upload.new(file: params[:file])

    byebug

    if @upload.save
      respond_to do |format|
        format.json { render json: {upload: @upload, url: @upload.file.url(:avatar) } }
      end
    else
      respond_to do |format|
        format.json { render json: {}, status: :unprocessable_entity }
      end
    end
  end
end

And here is the RSpec code I am using in the test to attach the file 这是我在测试中使用的RSpec代码来附加文件

filepath = "#{Rails.root}/spec/support/fixtures/uploads/flickr.jpg"
attach_file :file, filepath

click_on "Submit"

expect(page).to have_css(".avatar-img img")

When I run the test, the entire request goes through (Capybara does not indicate it had trouble finding the file or the form field). 当我运行测试时,整个请求都会通过(Capybara并不表示它无法找到文件或表单字段)。 However, the test fails. 但是,测试失败了。 And when I use byebug to inspect the uploads controller at the point it receives the request to save a new upload, there are no parameters being sent. 当我使用byebug检查上传控制器时它收到保存新上传的请求,没有发送任何参数。 As in params[:file] evaluates to nil , when it should have the file information for flickr.jpg . 如同params[:file]计算结果为nil ,它应该有flickr.jpg的文件信息。 Any idea why my file isn't attaching in the test, even though Capybara isn't raising any errors about it. 知道为什么我的文件没有附加在测试中,即使Capybara没有提出任何关于它的错误。

So after reading through this post I found an answer: 所以看完这篇文章后我找到了答案:

Capybara webkit doesn't pass params from angular Capybara webkit不会从有角度传递params

It looks like the problem came from the fact that I was using PATCH to send the uploads. 看起来问题来自我使用PATCH发送上传的事实。 After changing the method to PUT, my tests started passing. 将方法更改为PUT后,我的测试开始通过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM