Cucumber Capybara FactoryGirl Carrierwave无法附加文件

Cucumber Capybara FactoryGirl Carrierwave无法附加文件,cucumber,capybara,carrierwave,factory-bot,Cucumber,Capybara,Carrierwave,Factory Bot,我试图用黄瓜和水豚来测试我的应用程序。 我有以下步骤定义: 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)

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

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
编码:UTF-8 类ArticleImageUploader 但每次运行此场景时,都会收到错误消息:

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'
give我用有效的文章数据填写创建文章表单#features/step_definitions/blog_owner_creating_article.rb:1
无法附加文件,/uploads/article/image/1/example.jpg不存在(Capybara::FileNotFound)
/features/step_definitions/blog_owner_creating_article.rb:5:in'block(2层)in'
./features/step\u definitions/blog\u owner\u creating\u article.rb:3:in`/^I用有效的文章数据$/'填写create article表单
功能/博客\u所有者\u创建文章。功能:13:在“给定”中,我用有效的文章数据填写创建文章表单
在rails测试控制台中运行
FactoryGirl.build(:article)
时,我还发现FactoryGirl返回
image:nil


谁能解释一下我做错了什么

您需要直接传入路径:

attach_file('article_image', File.join(Rails.root, '/spec/support/example.jpg'))
这里发生的事情是,
attach\u file
需要一个字符串,而不是CarrierWave上传程序。当您将上载程序(
@article\u attributes.image
)传递给它时,
attach\u file
正在调用
上载程序#to_s
,调用
上载程序#path
。由于您尚未保存文章,上载图像的路径无效


还要注意,调用变量
@article\u attributes
会让人困惑,因为它实际上是一个完整的article对象,而不仅仅是一个散列。如果这是您想要的,您可以尝试(:article)的
FactoryGirl.attributes\u

Thaks回答!正如您所说,我曾尝试使用(:article)
@article\u attributes=FactoryGirl.attributes\u。但是
@article\u attributes[:image]
返回
。有没有办法将其转换为直接字符串路径?`
@article\u attributes[:image]。路径
?如果没有办法,那将是一个疯狂的世界。只是一个小小的警告,因为我花了一些时间才弄清楚我的测试失败的原因:如果你的工厂用
Rack::Test::UploadedFile
填充上传,路径可能不是你期望的
UploadedFile
将某种指纹添加到文件名中,因此“dummy.jpg”变为e。g、 "dummy.jpg20140221-25167-11iad2p'(可能是因为它将所有文件存储在/tmp中)。这可能会使您的扩展白名单出错,并使您的测试失败。如果发生这种情况,使用文件路径本身而不是FactoryGirl生成的属性调用
attach_file
可能是更好的解决方案。