用水豚附加文件

Attaching a file with capybara

我在使用水豚附加文件时遇到问题。

我有一个客户端模型

class Client
  mount_uploader :logo, ClientUploader
  validates :name, :logo, presence: true
end

在我看来,我将表单分成多个选项卡,徽标位于媒体选项卡上,并且由 javascript 切换,所以我使用的是 poltergeist javascript 驱动程序。

it "should allow a client to be created", js: true do
  click_link "New Client"
  fill_in "Name", with: "My Client"
  click_link "Media"
  attach_file("client_logo", File.join(Rails.root, "spec/support/images/landscape_image.jpg"))
  click_button "Create Client"
  expect(page).to have_content("Client was successfully created.")
end

控制器

class ClientsController
  def create
    @client = Client.new(client_params)
    if @client.save
      redirect_to clients_url, notice: 'Client was successfully created.'
    else
      render :new
    end
  end

  def client_params
    params.require(:client).permit(:name, :logo, :remote_logo_url, :logo_cache, :website, :display)
  end
end

我已经使用了 launchy save_and_open_page 并且它到达了正确的选项卡,所以该字段在那里(如果不是,水豚会抱怨)并且图像路径是正确的(再次,水豚抱怨如果不是)。但是我收到以下错误:

1) Creating A Client should allow a client to be created
     Failure/Error: expect(page).to have_content("Client was successfully created.")
       expected to find text "Client was successfully created." in "MANAGE YOUR CONTENT Clients Site Navigation Site Navigation Stored Images Stored Documents Site Settings View Website Manage Administrators Logout New Client SEE ALL CLIENTS 1 error prohibited this client from being saved: Logo can't be blank ContentMediaSettings Name Website Site Structure Stored Documents Stored Images"

我很困惑,有人有什么想法吗?

当与 PhantomJS 2.0.0 一起使用时,文件上传在 poltergeist 中不起作用。它已在 2.0.1 中修复(如果它曾经发布过)。如果您使用的是 2.0.0,您有几个选择,恢复到 1.9.8,构建您自己的 2.0.1 副本,或者换成使用 selenium 或 capybara-WebKit 进行这些测试