【问题标题】:Capybara FactoryGirl Carrierwave cannot attach fileCapybara FactoryGirl Carrierwave 无法附加文件
【发布时间】:2013-05-11 21:56:19
【问题描述】:

我正在尝试用黄瓜和水豚测试我的应用程序。 我有以下步骤定义:

Given(/^I fill in the create article form with the valid article data$/) do
  @article_attributes = FactoryGirl.build(:article)
  within("#new_article") do
    fill_in('article_title', with: @article_attributes.title)
    attach_file('article_image', @article_attributes.image)
    fill_in('article_description', with: @article_attributes.description)
    fill_in('article_key_words', with: @article_attributes.key_words)
    fill_in('article_body', with: @article_attributes.body)
  end

我的文章工厂是这样的:

FactoryGirl.define do
  factory :article do
    sequence(:title) {|n| "Title #{n}"}
    description 'Description'
    key_words 'Key word'
    image { File.open(File.join(Rails.root, '/spec/support/example.jpg')) }
    body 'Lorem...'
    association :admin, strategy: :build
  end
end

这是我的上传文件:

# encoding: UTF-8
class ArticleImageUploader < CarrierWave::Uploader::Base
  storage :file
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
  def extension_white_list
    %w(jpg jpeg gif png)
  end
end

但是每次我运行这个场景时,我都会收到错误消息:

Given I fill in the create article form with the valid article data # features/step_definitions/blog_owner_creating_article.rb:1
      cannot attach file, /uploads/article/image/1/example.jpg does not exist (Capybara::FileNotFound)
      ./features/step_definitions/blog_owner_creating_article.rb:5:in `block (2 levels) in <top (required)>'
      ./features/step_definitions/blog_owner_creating_article.rb:3:in `/^I fill in the create article form with the valid article data$/'
      features/blog_owner_creating_article.feature:13:in `Given I fill in the create article form with the valid article data'

我还发现,当我在 Rails 测试控制台中运行 FactoryGirl.build(:article) 时,FactoryGirl 返回 image:nil

谁能解释一下我做错了什么,好吗?

【问题讨论】:

    标签: cucumber capybara carrierwave factory-bot


    【解决方案1】:

    需要直接传入路径:

    attach_file('article_image', File.join(Rails.root, '/spec/support/example.jpg'))
    

    这里发生的情况是 attach_file 需要一个字符串,而不是 CarrierWave 上传器。当您将其传递给上传者 (@article_attributes.image) 时,attach_file 正在调用 Uploader#to_s,后者调用 Uploader#path。由于您还没有保存文章,所以上传图片所在的路径无效。

    还要注意,调用变量@article_attributes 会造成混淆,因为它实际上是一个完整的文章对象,而不仅仅是一个哈希。如果这是你想要的,你可能想试试FactoryGirl.attributes_for(:article)

    【讨论】:

    • 感谢您的回答!正如你所说,我试图使用@article_attributes = FactoryGirl.attributes_for(:article)。但是@article_attributes[:image] 返回#&lt;File:/home/olebi/Work/blog_engine/spec/support/example.jpg&gt;。有什么办法可以将其转换为直接字符串路径?`
    • @article_attributes[:image].path?如果没有办法to get the path from a File object,那将是一个疯狂的世界。
    • 请注意,因为我花了一些时间才弄清楚我的测试失败的原因:如果您的工厂使用Rack::Test::UploadedFile 填充上传,则路径可能不是您所期望的。 UploadedFile 在文件名中添加了某种指纹,所以 'dummy.jpg' 变成了 e。 G。 'dummy.jpg20140221-25167-11iad2p'(可能是因为它将所有文件都存储在 /tmp 中)。这可能会使您的扩展程序白名单跳闸并使您的测试失败。如果发生这种情况,使用文件路径本身而不是 FactoryGirl 生成的属性调用 attach_file 可能是更好的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-25
    • 2011-09-18
    • 1970-01-01
    相关资源
    最近更新 更多