Welcome to Giant Robots Smashing Into Other Giant Robots — a weblog about development, business, design and technology — written by thoughtbot.
its not your responsibility
Sometimes in Rails you ask yourself: how do I test this?
One example is ActiveRecord associations.
1 2 3 4 5 6 7 8 |
class Post < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base end |
Now from the documentation for ActiveRecord::Base#has_many we know it’s going to generate a method for us called #comments, so let’s test that.
1 2 3 4 5 |
def test_should_have_many_comments post = Post.new assert_respond_to post, :comments end |
Here all we’re testing is that a Post object understands #comments.
Maybe thats not enough?
1 2 3 4 5 6 |
def test_should_have_many_comments post = Post.new assert_respond_to post, :comments assert_kind_of Array, post.comments end |
Now we’ve added an additional assertion to check that the object returned from Post#comments is an Array.
Can we take it any further?
1 2 3 4 5 6 7 8 |
def test_should_have_many_comments post = Post.new assert_respond_to post, :comments assert_kind_of Array, post.comments assert_raises(ActiveRecord::AssociationTypeMismatch) { post.comments << Post.new } assert_nothing_raised { post.comments << Comment.new } end |
Now we’ve added 2 additional assertions that check that the object returned from Post#comments only allows Comment objects to be added to it via #<<.
OK, so maybe that might have been overkill, but at the very least you should check the object understands the given message a la our first attempt.
Another example is testing methods added to a class via a plugin.
1 2 3 4 5 |
class Article < ActiveRecord::Base acts_as_solr end |
From the acts_as_solr documentation it looks like calling the acts_as_solr class method on an ActiveRecord::Base class object will add a #find_by_solr method to it.
Here’s our test.
1 2 3 |
def test_should_understand_find_by_solr assert_respond_to Article, :find_by_solr end |
There is no need to test specific solr queries and then run assertions on the results. Once you go that route, you are now testing the acts_as_solr plugin and the solr web application; this is not your application’s responsibility, its the author(s) of acts_as_solr and solr to test their own code.
You’re also introducing an external dependency in your tests (that a Java application server is running the solr web application). Like any external dependency, this is going to slow your tests down considerably. And once your tests start getting slower developers are not going to be running them as much. And if you aren’t running your tests your code is going to go south.
About this entry
You're reading an entry on GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS, the company weblog of thoughtbot, inc.
- Author:
- Jared Carroll
- Published:
- September 18th 08:00 PM
- Updated:
- September 30th 09:57 AM
- 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; Jester, a REST/ActiveResource client library 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.
6 comments
Jump to comment form