Reputation: 273
I am using Carrierwave and Minimagick for image uploads to a user's profile photo. I have followed the directions on Carrierwave's readme of how to create an uploader and mount it. I am testing with Rspec and Capybara.
Here is my user_profile_spec.rb, relevant lines:
feature 'Visitor views profile page' do
before(:each) do
@user = sign_in
click_link "Profile"
end
scenario 'can upload a photo' do
attach_file 'photo', File.join(Rails.root, 'public', 'images', 'default.png')
click_button "Update Profile"
expect(page).to have_content "default.png"
end
Here is my _form.html.erb for the user profile edit page:
<%= form_for @profile, url: @profile, :html => {:multipart => true} do |f| %>
<strong>Photo:</strong>
<%= image_tag @profile.photo.display if @profile.photo? %>
</p>
<div class="field">
<%= f.label :photo %>
<%= f.file_field :photo %>
....
<% end %>
And my error:
1) Visitor views profile page can upload a photo
Failure/Error: attach_file 'photo', File.join(Rails.root, 'public', 'images', 'default.png')
Capybara::ElementNotFound:
Unable to find file field "photo"
# ./spec/features/profiles/user_profile_spec.rb:35:in `block (2 levels) in <top (required)>'
I have tried changing the attach_file part to :photo
, changing the file attached, using my Factorygirl profile model which has a photo already uploaded:
FactoryGirl.define do
factory :profile do
website 'http://www.validwebsite.com'
country 'Valid country'
about 'Valid about statements that go on and on'
profession 'Validprofession'
age 22
user
photo { Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec', 'support', 'profile_photos', 'default.png')) }
end end
Couldn't sort out the error. Is it a glitch with carrierwave?
Here is my photo_uploader.rb, don't think its necessary but just in case, all the information.
class PhotoUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def default_url
ActionController::Base.helpers.asset_path("images/" + [version_name, "default.png"].compact.join('_'))
"/images/" + [version_name, "default.png"].compact.join('_')
end
version :display do
process :resize_to_fill => [150, 150]
end
version :thumb do
process :resize_to_fill => [50, 50]
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
Edit: Added some new information about the spec.
Upvotes: 3
Views: 2285
Reputation: 2414
In your current spec, attach_file is looking for an id, name or label matching "photo", which you apparently don't have. Any of these should work, instead:
attach_file 'profile[photo]', File.join(Rails.root, 'public', 'images', 'default.png') # name
attach_file 'profile_photo', File.join(Rails.root, 'public', 'images', 'default.png') # id
attach_file 'Photo', File.join(Rails.root, 'public', 'images', 'default.png') # label
Upvotes: 2