用capybara附加文件

时间:2015-10-08 13:32:11

标签: ruby-on-rails capybara rspec-rails poltergeist

我在使用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"

我很难过,有没有人有任何想法?

1 个答案:

答案 0 :(得分:1)

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