This post originated from an RSS feed registered with Ruby Buzz
by Rick DeNatale.
Original Post: Rails Integration Test File Upload Plugin
Feed Title: Talk Like A Duck
Feed URL: http://talklikeaduck.denhaven2.com/articles.atom
Feed Description: Musings on Ruby, Rails, and other topics by an experienced object technologist.
Some time ago, I wrote a rails patch which allows http file uploading in integration tests.
You just use the same TestUploadedFile class used in functional tests or controller specs if you’re an RSpec’er. So you can do something like.
post '/upload' :file => TestUploadedFile("/path/to/blah.txt")
The patch detects a TestUploaded file amongst the parameters, and converts the request into a proper multi-part request.
It will work for both posts and updates.
If you’re running edge rails you’ve already got this. But many have asked for a version for earlier rails versions. At work, I’ve been working on an RSpec story which requires file uploading, and we’re using the 2.0.2 gem version of Rails for deployment purposes, so I whipped up a little plugin which patches 2.0.2 Rails with the changes from Rails changeset 8978. The plugin checks the rails version and throws an exception if it’s anything other than “2.0.2” since I haven’t tried this with anything earlier, and it’s not necessary for anything newer.