Testing with rSpec

553 views
Skip to first unread message

bertly_the_coder

unread,
Jun 9, 2012, 9:59:27 AM6/9/12
to dragonf...@googlegroups.com
Hi guys,

Does anyone have any advice on how to go about testing an "image upload" in rails? I'm eventually going to store my images on S3. I currently have a validation that checkes that validates the presence of :image, but my database contains the field :image_uid, so this fails.How do I resolve the two and do I have to upload to S3 to test efficiently?

Thanks guys.

Ramon

unread,
Jun 9, 2012, 8:39:02 PM6/9/12
to dragonf...@googlegroups.com
Is there an issue with validating presence of image or image_uid? It could be made into a validator to make prettier, but it'll be something like this:

validates :image, presence: true, if: proc {|m| m.image_uid.nil?}
validates :image_uid, presence: true, if: proc {|m| m.image.nil?}

Regarding s3, this might help:

bertly_the_coder

unread,
Jun 9, 2012, 11:54:11 PM6/9/12
to dragonf...@googlegroups.com
Hey Ramon, the validators provided with the gem are 
validates_presence_of :cover_image
validates_size_of :cover_image, :maximum => 500.kilobytes
validates_property :format, :of => :cover_image, :in => [:jpeg, :png, :gif]
  # ..or..
validates_property :mime_type, :of => :cover_image, :as => 'image/jpeg', :case_sensitive => false
validates_property :width, :of => :cover_image, :in => (0..400), :message => "é demais cara!"
Why did you veer off and test the with the uid? Is that the correct way to do it? I guess my question is; how do I incorporate theses provided validators into my tests? Especially the first one, as there's no column :image/:cover_image etc depending on your model name...

Ramon Tayag

unread,
Jun 10, 2012, 12:23:50 AM6/10/12
to dragonf...@googlegroups.com
Honestly, I don't know. If dragonfly fly has a validator for that, then perfect :)

Ramon Tayag

bertly_the_coder

unread,
Jun 10, 2012, 1:07:38 AM6/10/12
to dragonf...@googlegroups.com
Haha! Thanks for taking a stab at it. I just want to keep it as simple as I can, no use reinventing validations if they are already in there. 

bertly_the_coder

unread,
Jun 10, 2012, 1:51:19 AM6/10/12
to dragonf...@googlegroups.com
D'oh, I got it.......
The validation is indeed -> validates_presence_of :image
to the model add -> image_accessor :image, (which is the provided reader/writer)
to the factory create the file that you'd like to upload ->  image File.new("spec/assets/images/sample_rails.png")

run the tests and dragonfly will figure it out. Thanks for your help!!


On Saturday, June 9, 2012 4:59:27 PM UTC+3, bertly_the_coder wrote:
On Saturday, June 9, 2012 4:59:27 PM UTC+3, bertly_the_coder wrote:
On Saturday, June 9, 2012 4:59:27 PM UTC+3, bertly_the_coder wrote:
On Saturday, June 9, 2012 4:59:27 PM UTC+3, bertly_the_coder wrote:

Radhika Bhatt

unread,
Jun 13, 2014, 7:33:12 AM6/13/14
to dragonf...@googlegroups.com
Hey @bertly_the_coder, I was scratching my head since 2days. Finally I am done with tests due this simple solution. Thanks a ton for this thread!
Reply all
Reply to author
Forward
0 new messages