Welcome to Giant Robots Smashing Into Other Giant Robots — a weblog about development, business, design and technology — written by thoughtbot.
Testing paperclip with Shoulda
Josh Susser had a bunch of great things to say about Shoulda in his RailsConf 2008 presentation, The great test framework dance-off. One of the parts he really liked was the conceptual simplicity of creating macros when drying up your tests.
It’s a misnomer to even call what you do with Shoulda “macros”, since they’re just normal class methods. Here’s one we use all the time for our projects that use Paperclip (which you should definitely check out if you haven’t all ready).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# in test_helper.rb... class Test::Unit::TestCase def self.should_have_attached_file(attachment) klass = self.name.gsub(/Test$/, '').constantize context "To support a paperclip attachment named #{attachment}, #{klass}" do should_have_db_column("#{attachment}_file_name", :type => :string) should_have_db_column("#{attachment}_content_type", :type => :string) should_have_db_column("#{attachment}_file_size", :type => :integer) end should "have a paperclip attachment named ##{attachment}" do assert klass.new.respond_to?(attachment.to_sym), "@#{klass.name.underscore} doesn't have a paperclip field named #{attachment}" assert_equal Paperclip::Attachment, klass.new.send(attachment.to_sym).class end end end # And in the Tests... class UserTest < Test::Unit::TestCase should_have_attached_file :avatar end |
One of Shoulda’s main goals is to remain as simple to understand and extend as possible. While there’s a little bit of magic when getting the current class from the test class name, the rest of the macro is concise and easy to understand.
We use this technique liberally throughout our test suites on a variety of applications, and it’s worked wonders. We even push the general purpose ones into Shoulda proper. If you have any that you think should be shared with the community, just send us a git pull request.
About this entry
You're reading an entry on GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS, the company weblog of thoughtbot, inc.
- Author:
- Tammer Saleh
- Published:
- June 3rd 03:56 PM
- Updated:
- June 3rd 04:12 PM
- Sections:
- Development
thoughtbot is hiring
We are hiring web developers and web designers in both Boston and New York, NY.
What are we up to?
We built Shoulda, an eclectic set of additions to Test::Unit; Paperclip to manage uploaded files without hassle; factory_girl a replacement for Rails fixtures; Jester, a REST/ActiveResource client written in Javascript, and Squirrel, an enhancement for ActiveRecord's find syntax; — amongst some other projects.

Chad (President) and Jon (CTO) co-authored a technical book titled Pro Active Record: Databases with Ruby and Rails, which explores the ins and outs of the ActiveRecord ruby library. You can buy it today at Amazon.com.
About thoughtbot, inc.
We are a small web application development consulting business, with offices in Boston, MA and New York, NY. If you're looking to find a team for your next web development project or your new web application — get in touch.
7 comments
Jump to comment form