I’m driving out a new photo upload feature for a client and can’t figure out how to trigger the file upload in my feature so that I can test it. I see from PaperClip::Shoulda::Matchers how to test it once I’ve got it loaded, but can’t seem to load the file. My feature spec goes like this:
scenario 'offers an upload item' do
@accident_report = create :accident_report
visit accident_reports_path
expect(page).to have_content "Auto Accident Report List"
visit '/accident_reports/1'
expect(page).to have_content "Driver's Accident Report"
expect(attach_file('upload_file_name', 'spec/factories/test_photo_1.png')).to be_true
The ‘attach_file’ method is from Capybara, but can’t find the field; I get the error:
cannot attach file, no file field with id, name, or label 'upload_file_name' found
I got the same thing if I simply specified ‘upload’ for the file locator.
Edit: I’m guessing this is the approach? http://stackoverflow.com/questions/7260394/test-a-file-upload-using-rspec-rails
Edit 2: Got it working with the following:
File.open('spec/factories/test_photo_1.png') { |file| @accident_report.upload = file }
put accident_report_path(@accident_report.id)
PaperClip needs a file handle to work its magic and then update record to persist the information, if needed.
Edit 3: FYI, I am unable to get Capybara’s attach_file method to work. Using:
file = attach_file('accident_report_upload', Rails.root.join('spec','factories','test_photo_1.png'))
just results in a reference to ‘/images/original/missing.png’