-1

Possible Duplicate:
test a file upload using rspec - rails

I don't know how to write test cases for file uploads. can you please help me

I have form with user_photo, name, email. i write test cases for name and email

like

 fill_in "user_name", :with => "xyz"
 fill_in "user_email", :with => "test@test.com"

but how can i write test cases for select photo ?

Community
  • 1
  • 1
Dipak Panchal
  • 5,996
  • 4
  • 32
  • 68

2 Answers2

1

If you have used paperclip then I can help you in this..

Write your test case like this

def do_upload_file
# write the request for the action as defined in your routes file
# get :upload_file, :id => :user_id (id of the user)
end

before do
 # set the session variables and other parameters here..
end

it "should upload the file correctly" do
  @user.should_receive(:update_attributes).and_return(true)
  do_upload_asset
  response.should redirect_to(whatever_path)
end

Works for me and can hopefully work for you too....

Aditya Kapoor
  • 1,542
  • 10
  • 13
1

You can do that using:

obj.asset = fixture_file_upload('/dummy_file.png', 'image/png')

where you can define the fixture path in your spec helper as :

config.fixture_path = "#{Rails.root}/spec/fixtures"

Official documentation

Rahul garg
  • 9,202
  • 5
  • 34
  • 67