Reputation: 352
I'm trying to write an integration test for a file upload using AWS S3. The behavior I'm trying to test is that a user is supposed to click the "Choose Files" button, then choose a video, then the user is supposed to see the video and the Video count is supposed to increase by 1. I'm stumbling at the starting gates here, and can't even seem to have the user click the button. I'm using the s3 direct upload gem and the javascript I'm using for the form is
<script id="template-upload" type="text/x-tmpl">
My output HTML looks something like this
<form accept-charset="UTF-8" action="https://bucketname-bucket.s3.amazonaws.com/" data-callback-method="POST" data-callback-param="video[direct_upload_url]" data-callback-url="http://0.0.0.0:3000/videos" enctype="multipart/form-data" id="s3-uploader" method="post">
***some AWS stuff***
<input id="file" multiple="multiple" name="file" type="file">
So if you actually go to the page, there is a button that says "Choose Files." If I click on that, it lets me choose a file to upload. However, when I put in rspec
click_button "Choose Files"
it says it can't find the button. So the obvious question is, how do I have it pretend to click the button?
Upvotes: 4
Views: 3275
Reputation: 15957
You didn't really supply anything about your form in Ruby and the HTML that form generates or the test you've written so I'm going to give you just an example.
let(:user) { FactoryGirl.create(:user) }
let!(:project) { FactoryGirl.create(:project) }
before(:each) do
sign_in_as(user)
end
scenario 'a registered user logins and creates a new music project' do
count = Project.all.count
visit new_project_path
fill_in 'Title', with: "Chillwave Song"
fill_in 'Description', with: "I started this after being inspired by a Tycho concert"
attach_file('project[project_file]', File.join(Rails.root, '/spec/support/project.zip'))
click_button('Submit')
expect(page).to have_content('Project created successfully!')
expect(page).to have_content('I started this after being inspired by a Tycho concert')
expect(Project.all.count).to eq(count + 1)
end
So something should jump out at you here - do you really want to upload a file every time you run your tests? The answer should be no, so how do you 'fake' this upload process? Really the best bit of advice is to use this in your Factory:
FactoryGirl.define do
factory :attachment do
file ActionDispatch::Http::UploadedFile.new(:tempfile => File.new("#{Rails.root}/spec/support/project.zip"), :filename => "project.zip")
end
end
Upvotes: 7