我正在使用 Carrierwave 和 Minimagick 将图像上传到用户的个人资料照片。我已按照Carrierwave 自述文件中有关如何创建上传器和安装它的说明进行操作。我正在使用 Rspec 和 Capybara 进行测试。
这是我的 user_profile_spec.rb,相关行:
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
这是用户个人资料编辑页面的 _form.html.erb:
<%= 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 %>
我的错误:
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)>'
我尝试:photo
使用已上传照片的 Factorygirl 个人资料模型将 attach_file 部分更改为 ,更改附加文件:
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')) }
结束结束
无法解决错误。是载波的故障吗?
这是我的 photo_uploader.rb,认为没有必要,但以防万一,所有信息。
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
编辑:添加了一些关于规范的新信息。