Using fixture_file_upload method Rails 4 Rspec

时光毁灭记忆、已成空白 提交于 2019-12-24 00:18:49

问题


I am testing the creation of my Image object with FactoryGirl and Rspec.

Orignally i have been doing I am using

photo { File.new(File.join(Rails.root, 'spec/fixtures', photo_name)) }

but all of a sudden i am getting

Paperclip::AdapterRegistry::NoHandlerError:
No handler found for "#<File:0x00000003bf15c8>

So after reading various posts I have changed to fixture_file_upload method but am having trouble creating the object.

images.rb

include ActionDispatch::TestProcess
FactoryGirl.define do
  factory :image do
    transient do
      photo_name 'validated_image.jpg'
   end
   photo { fixture_file_upload(Rails.root.join('spec', 'fixtures', photo_name), 'image/jpeg') }
end

Though originally i tried this as the fixture path was declared in my rails_helper

photo { fixture_file_upload photo_name, 'image/jpeg' }
# config.fixture_path = "#{::Rails.root}/spec/fixtures" # rails_helper
# Would produce error 'validated_image.jpg file does not exist'

My current implementation (vey top) doesnt create an instance of Image when doing this

it 'creates a new image' do
  expect do
    post :create, image: FactoryGirl.attributes_for(:image)
  end.to change(Image, :count).by(1)
end
# ERROR
# expected #count to have changed by 1, but was changed by 0

Am i missing anything here?

Thanks

来源:https://stackoverflow.com/questions/34677780/using-fixture-file-upload-method-rails-4-rspec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!