回形针::使用FactoryGirl时附件作为字符串传递 [英] Paperclip::Attachment passed as string when using FactoryGirl

查看:65
本文介绍了回形针::使用FactoryGirl时附件作为字符串传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用FactoryGirl 3.0在Rails 3.2.6上为回形针3.1.2创建功能测试,当我通过文件附件时,我的控制器接收到一个字符串,该字符串是文件位置,而不是Paperclip :: Attachment对象.

I am trying to create a functional test for paperclip 3.1.2 on rails 3.2.6 using FactoryGirl 3.0 and when i pass the file attachment my controller receives a string which is the file location and not the Paperclip::Attachment object.

我的工厂是:

include ActionDispatch::TestProcess
FactoryGirl.define do
   factory :artist do
    id 1
    name 'MyString'
    photo {fixture_file_upload("#{Rails.root}/test/fixtures/files/rails.png",'image/png')}
  end
end

我的测试是:

test "should create artist" do
  assert_difference('Artist.count') do
    post :create, :artist => { name: @artist.name, photo: @artist.photo }, :html => { :multipart => true }
  end

  assert_redirected_to artist_path(assigns(:artist))
end

在我的控制器中,这是

params[:artist][:photo].class.name 

等于字符串",但是当我将其添加到测试中时,该字符串就会通过

equals "String", but this passes when i add it to the test

assert @artist.photo.class.name == "Paperclip::Attachment"

我当前的解决方法是在测试中创建Fixture_file_upload:

My current workaround is to create the fixture_file_upload in the test :

test "should create artist" do
  assert_difference('Artist.count') do
    photo = fixture_file_upload("/files/rails.png",'image/png')
    post :create, :artist => { name: @artist.name, photo: photo }, :html => { :multipart => true }
  end

  assert_redirected_to artist_path(assigns(:artist))
end

可以工作,但是会创建一个"Rack :: Test :: UploadedFile",所以我很困惑为什么当调用相同的方法创建两个工厂时,为什么我的工厂返回"Paperclip :: Attachment".

which works but creates a "Rack::Test::UploadedFile" and so I'm pretty confused why my factory returns a "Paperclip::Attachment" when the same method is called to create them both.

预先感谢您提供的所有信息,因为我显然想使用工厂,而不是在工厂外部定义fixture_file_upload.

Thanks in advance for any light that you can shed on this as obviously I would like to use the factory rather than defining the fixture_file_upload outside of it.

推荐答案

您是否尝试替换

photo {fixture_file_upload("#{Rails.root}/test/fixtures/files/rails.png",'image/png')}

photo File.new("#{Rails.root}/test/fixtures/files/rails.png")

在工厂内部.

这篇关于回形针::使用FactoryGirl时附件作为字符串传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆