Guest User

Testing file uploads in Rails: can't convert ActionController::TestUploadedFile into String

a guest
Feb 23rd, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. class ProfilesControllerTest < ActionController::TestCase
  2. test "creates a new profile" do
  3. fixture_image = fixture_file_upload("#{RAILS_ROOT}/test/fixtures/files/avatar.jpg", 'image/jpeg')
  4. post :create, :profile=>{:username=>'johndoe',
  5. :password=>'mypass',
  6. :avatar => fixture_image
  7. }, :html => { :multipart => true }
  8. assert_response :success
  9. assert_not_nil Profile.find_by_username("johndoe")
  10. assert_not_nil Profile.find_by_username("johndoe").avatar
  11. end
  12. end
  13.  
  14. @profile = Profile.new(params[:profile])
  15. @profile.save
  16.  
  17. class Profile
  18. include MongoMapper::Document
  19. plugin Joint
  20.  
  21. attachment :avatar
  22. end
  23.  
  24. 1) Error:
  25. test_creates_a_new_profile(Api::ProfilesControllerTest):
  26. TypeError: can't convert ActionController::TestUploadedFile into String
  27. (eval):15:in `size'
  28. (eval):15:in `avatar='
  29. /Users/oliver/.rvm/gems/ruby-1.8.7-p302/gems/mongo_mapper-0.8.6/lib/mongo_mapper/plugins/keys.rb:183:in `send'
  30.  
  31. def uploaded_file(path)
  32. pathname = Rails.root + 'test/fixtures/' + path
  33. filename = File.basename(path)
  34. tempfile = Tempfile.new(filename)
  35. content_type = MIME::Types.type_for(pathname.to_s).to_s
  36.  
  37. FileUtils.copy_file(pathname, tempfile.path)
  38.  
  39. (class << tempfile; self end).class_eval do
  40. alias local_path path
  41. define_method(:original_filename) { filename }
  42. define_method(:content_type) { content_type }
  43. end
  44.  
  45. return tempfile
  46. end
Advertisement
Add Comment
Please, Sign In to add comment