Learning Ruby – TDD Style! (Part 1)

start | previous | next | finish

I’m starting to take a serious look at Ruby, and like many people who are new to a programming language, it’s difficult to know where to start. Sure I have the “pickaxe” book in hand and my bookmarks are overflowing with Ruby links, but where ever and however I start I would like to be able to see immediate results and I would like to prove to myself that I’m understanding the syntax.

How about testing what I already know? Well, right now I don’t know too much. However, that should make things easier since it will prevent me from wanting to take on too much at once and keeping myself focused on small obtainable goals.

Since I come from the Test Driven Development (TDD) camp, testing is pretty second nature and it is very comforting for me to keep on doing it. Perhaps the question I should be asking is: Where should one start when learning Ruby using TDD?

Since Ruby already has a unit testing framework, the first place we should look is the Test::Unit module. Hmm.. looks like there is a TestCase class. In the past when I developed Java applications, I’ve used JUnit extensively so this seems familiar to me. Okay, but what are we going to test first? Well, when you are writing a test you typically need to make an assertion. Right, where is that kept?

After digging around in the ruby docs online, we see that the Test::Unit::Assertions class contains the assertions used by Test::Unit:TestCase. Scanning down the document, the first public instance method we encountered is assert. Well, golly gee whiz. If we were pairing with Donald Rumsfeld then he would tell us to be vigilant and go after that method because its a known-unknown.

Alright, lets start by creating an AssertionsTest class which will test the assert behavior.

require 'test/unit'

class AssertionsTest < Test::Unit::TestCase

  def test_should_do_something_with_assert
    # method to be revisited . . .
  end

end

This seems to be a good breaking point. My goal is to treat this as a running example while I’m learning the finer aspects of Ruby. Hopefully readers like yourself will find this to be a useful and educational experience.

Advertisements

3 Responses to Learning Ruby – TDD Style! (Part 1)

  1. Josh Graham says:

    G’day Ross,

    Looking forward to more articles! Just noticed that the Next link on your first part has the date portion wrong in the URI path – might be a wordpress bug? The second part links back just fine.

    Cheers,
    Josh

  2. Ross Niemi says:

    Thank Josh.

    I’m afraid that the Next link problem was a copy and paste error on my part and not WordPress. Thanks for the catch. In the future I’ll try to be better in… ahem… testing my blogs 🙂 TDD with Selenium, perhaps? 🙂

  3. Anton says:

    Thanks for nice articles. I live far away from centers of computer science industry, just started to dig into ruby. I wonder where people usually use ruby? I found this “Ruby on Rails”, and its great, but are there any other areas where people prefer ruby to other languages? curious..
    i found it very difficult to test complex applications with ruby on rails fixtures, right now I’m trying to find something easier

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: