A Testy Way of Learning Ruby

In my recentpost, I started sharing some of the fun of learning Ruby using Test-Driven techniquesto learn the language features and document what you are learning as you go.

There are several advantages of learning the language this way. It flexes your brainin ways that just reading the book or typing in the sample code doesn’t. Once you’redone, you have some running, organized code that you can refer back to. Also, whenyou upgrade your language, or change which platform you run it on, you can run yoursuite of learning tests. Find out if Ruby 1.9 is different from 1.8…and how. Are thereimplementation differences between linux Ruby and Iron Ruby? You’ll know.

Today, I’d like to cover the additional assert variants available in the Test::Unitmodule, and talk about using an IDE.

Lasttime, we covered the assert method for testing truth. But there are lot more thingsyou can test!

  • assert – Tests the truth of an expression
  • assert_block – Tests that the return value of a code block is true
  • assert_equal – Tests that two values are equal
  • assert_in_delta – Tests two floats with tolerance for floating point error (read upon this one! it’s a doozy)
  • assert_instance_of – Tests that an object is an instance of a class
  • assert_kind_of – Tests that an object is of a class, or a subclass, or implementsthe kind
  • assert_match – Test using a regular expression
  • assert_nil – Tests the nil-ness of the expression
  • assert_no_match – A not test
  • assert_not_equal – A not test
  • assert_not_nil – A not test
  • assert_not_same – A not test
  • assert_nothing_raised – A not test
  • assert_nothing_thrown – Not a test. Just kidding.
  • assert_operator – I don’t get this one. Well, I’m still learning!
  • assert_raise – Tests that the code block raises a given exception
  • assert_respond_to – Tests that an object has a named method defined
  • assert_same – Tests that both values are the same object instance
  • assert_send – Another one to learn on. Looks like it tests a method call?
  • assert_throws – Looks like another type of exception handling.

With this group of methods, it should be rather easy for you to write some nice, readabletests as you learn things up. One interesting one is assert_same, which can provethat all values of True are really the same instance. Also, when you write a testthat fails by raising an exception, use assert_raise to document that. You shouldhave a test that shows what happens when you divide by zero, and one that shows whathappens when you use an uninitialized variable.

Using an IDE

Most of the ‘True Ruby Folk’ believe with all their flinty hearts that you shouldn’tneed anything more than a charred and pointed stick to write excellent Ruby. Theyare probably right. On the other hand, a good IDE can be like a coding video gameand make the process so much more colorful and fun. I’ve sure found that JetBrains’new RubyMine IDE has made myearly Ruby coding a bit of clicky goodness.

Creating a new test class is really easy. You just add the file to your project:

You get a fully fleshed and voluptuous test class ready for you to have your way withit.

And while you’re learning the language, it’s really nice to have some code completionfeatures so that you can figure out the rest of what you’re typing without going madswitching between windows.

All that and it’s not going to break the bank. Just $99! Give it a try.

I hope you’re keeping up with your test writing. I’ve got 168 assertions testing languagefeatures and I’m just through the primitives and variable types.

How many tests can you write?