« Test-Driven Rails Studio | Main | Our take on presenting code »

May 14, 2008

Silly Useful shoulda trick

_mg_3035

Playing with the shoulda testing framework, I came across a small but useful trick. Because the tests are written inside closures, local class variables are available inside should blocks. They're only evaluated once, so they don't take the place of setup blocks, but they are a nice way of storing test-wide values. Somehow, I like the look of this better than using instance variables or constants—the tests seem to be more uniform and balanced.

require 'test/unit'
require 'shoulda'
require 'date' 

require 'csv_to_html/age_calculator'

class AgeCalculatorTest < Test::Unit::TestCase
                                  
  birth = Date.parse("2003-05-02")

  should "be year difference if now later in year than birth" do
    now = Date.parse("2008-06-15")
    assert_equal 5, AgeCalculator.age_given_dates(now, birth)
  end

  should "be year difference if now later in month than birth" do
    now = Date.parse("2008-06-15")
    assert_equal 5, AgeCalculator.age_given_dates(now, birth)
  end
         
  should "be year difference minus 1 if now earlier in year than birth" do
    now = Date.parse("2008-04-15")
    assert_equal 4, AgeCalculator.age_given_dates(now, birth)
  end

  should "be year difference minus 1 if now earlier in month than birth" do
    now = Date.parse("2008-05-01")
    assert_equal 4, AgeCalculator.age_given_dates(now, birth)
  end

end

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83451c41c69e200e5523e8c408834

Listed below are links to weblogs that reference Silly Useful shoulda trick:

Comments

That looks to me like a "clever" trick that's due to bite you in the butt in the long run, especially if you have more than one programmer working one the code over a long period of time. I did a little Google on clever programing and came up with these gems:

Everyone knows that debugging is twice as hard as writing a program in the first place. So if you are as clever as you can be when you write it, how will you ever debug it? -Kernighan

The competent programmer is fully aware of the strictly limited size of his own skull; therefore he approaches the programming task in full humility, and among other things he avoids clever tricks like the plague. -Dijkstra

Writing intention-revealing code is much more important than being clever. -Anonymous

Source: http://www.cse.iitb.ac.in/~kart/Quotes%20-%20Dev.html

Ted:

I'd be interested to know exactly what you feel makes this code hard to maintain, or “clever.” My personal believe is that it reads better than the alternatives.


Dave

Would be interested to hear why this is bad, "clever" usage. It's a simple, clear example of a closure; nothing scary, overly fancy, or hard to maintain/explain about it. It also looks like a pretty useful little approach.

If a person knows what a closure is, this should make immediate sense; if they don't know what a closure is, they should.

We use this technique liberally when developing custom shoulda macros. I'm not sure how to post code snippets, but if you look at the lib/shoulda/active_record.rb file, you'll find plenty of examples.

And I can understand how some people may find that technique to be tricky, but I'd encourage them to learn more about and make more use of ruby blocks. It's really an aspect of the language that sets it apart.

Cheers,
Tammer

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Now in Beta

  • Programming Ruby, 3rd Edition
    Third Edition, Covering Ruby 1.9, now available
My Photo

Pragmatic Stuff

Photos

  • www.flickr.com
    This is a Flickr badge showing public photos from pragdave tagged with pragdave_badge. Make your own badge here.

Site Search

  • Google Search

    The web
    PragDave