fixture_file_upload Example


/ Published in: Ruby
Save to your folder(s)

fixture_file_upload is another nifty Rails testing feature. It allows you to create "MIME-encoded content that would normally be uploaded by a browser input field." (Part in quotes straight from Agile Web Development with Rails, v2). Put your files to test in test/fixtures/files. You have to create the 'files' directory; rails doesn't do it for you.


Copy this code and paste it in your HTML
  1. # app/models/house_photo.rb
  2. class HousePhoto < ActiveRecord::Base
  3. belongs_to :house
  4. acts_as_list :scope => :house
  5. file_column :image,
  6. :root_path => ENV["RAILS_ENV"] == "test" ?
  7. File.join(RAILS_ROOT, "public", "test") :
  8. File.join(RAILS_ROOT, "public"),
  9. :magick => {:geometry => "640x480",
  10. :versions => {:thumb => "128x96", :medium => "288x288" }}
  11. validates_presence_of :image
  12. validates_presence_of :house_id
  13. end
  14.  
  15. # test/unit/house_photo_test.rb
  16. require File.dirname(__FILE__) + '/../test_helper'
  17. class HousePhotoTest < Test::Unit::TestCase
  18. fixtures :house_photos, :houses
  19.  
  20. def test_should_get_positioned
  21. p = create_photo
  22. assert p.position
  23. end
  24.  
  25. # Other tests go here...
  26.  
  27. protected
  28.  
  29. def create_photo(options = {}, image_filename = 'files/house1_exterior1.gif')
  30. photo = fixture_file_upload(image_filename, 'image/jpg')
  31. houses(:house1).house_photos.create({ :image => photo }.merge(options))
  32. end
  33. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.