Barefoot Development

The Joy of Rails Automated Testing

Many developers are not in the habit of adding unit and functional tests to their projects. With many frameworks, testing is a tedious addition that can get pushed aside because of time constraints. However, with Ruby on Rails, automated testing is baked right in, so there's no excuse for not using it. And, once you start using unit & functional tests, you will sleep better, have less stress, exercise more, lose weight, improve your marriage, and have more money to save for retirement. Well, all of those benefits may not come directly from automated testing, but you'll certainly build a much more high-quality application, and that can't hurt the rest of your life!

I'm not going to go in-depth on the basics of unit/functional testing here ... many other resources do a great job of that, such as the excellent Agile Web Development with Rails. However, I wanted to show a simple & quick way to add functional tests in response to daily development problems.

It happens all the time during development: you build a controller with a page and see "Action Controller: Exception caught" when you view it in your browser. You immediately move to the task of fixing whatever problem is revealed.

However, it only takes an extra minute to add a new functional test that will give you the added assurance that once you fix the problem, you'll never be surprised by it again because your test will automatically tell you if it happens again.

They key is in the very nice display that Rails renders when an exception is caught. Immediately following the error trace, Rails displays the current Request parameters. For example, I just received an error on a seach page with parameters like this:

Request

Parameters: {"commit"=>"Search", "office_id"=>"0", "school_name"=>nil, "first_name"=>nil, "last_name"=>nil}

The parameters are in a hash that is in the exact form needed by the get or post methods you use within a Rails functional test. So here's what you do:

1. Create a new method within the functional test file for your controller, something like this:

def test_search_empty_params
 # Use all empty fields
 get :search
 assert_response :success
 assert_template 'search'
 assert_not_nil assigns(:search_params)
 assert_not_nil assigns(:results)
end


2. Copy & paste the list of request parameters from the error page in your browser to the second parameter of the get method (this also works with the post method), so your test method ends up like this:


def test_search_empty_params
 # Use all empty fields
 get :search, {"commit"=>"Search", "office_id"=>"0", "school_name"=>nil, "first_name"=>nil, "last_name"=>nil}
 assert_response :success
 assert_template 'search'
 assert_not_nil assigns(:search_params)
 assert_not_nil assigns(:results)
end


Now, go on to fix whatever caused the problem in the first place, and run your functional test to not only make sure your problem is fixed, but that your fix didn't break anything else. Now, you'll be sure that the problem will never show up again, at least, not without a message from your test. Doesn't that bring you joy?

Doug Smith, Senior Developer, Barefoot

Labels: , , ,

0 comments

Post a Comment

« Home