2013-09-02 3 views
2

사용자 프로필 사진에 이미지를 업로드하는 데 Carrierwave 및 Minimagick을 사용하고 있습니다. 나는 업 로더를 만들고 마운트하는 방법에 관한 Carrierwave's readme에 관한 지침을 따라왔다. Rspec과 Capybara로 테스트하고 있습니다. 여기 페이지에 명시 적으로 존재하는 파일 "사진"필드를 찾을 수 없습니까? Rails 4, Minimagick, Carrierwave

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입니다 : 여기

내 user_profile_spec.rb, 관련 선입니다

<%= 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)>' 

attach_file 부분을 :photo으로 변경하고 첨부 된 파일을 변경하고 이미 업로드 된 사진을 가지고 Factorygirl 프로파일 모델 :

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 

편집 : 추가 사양에 대한 몇 가지 새로운 정보.

답변

1

현재 사양에서 attach_file은 분명히 가지고 있지 않은 "사진"과 일치하는 ID, 이름 또는 라벨을 찾고 있습니다. 대신 다음 중 하나를 사용해야합니다.

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