A Testy Way of Learning Ruby
In my recent post, I started sharing some of the fun of learning Ruby using Test-Driven techniques to 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 brain in ways that just reading the book or typing in the sample code doesn’t. Once you’re done, you have some running, organized code that you can refer back to. Also, when you upgrade your language, or change which platform you run it on, you can run your suite of learning tests. Find out if Ruby 1.9 is different from 1.8…and how. Are there implementation differences between linux Ruby and Iron Ruby? You’ll know.
Today, I’d like to cover the additional assert variants available in the Test::Unit module, and talk about using an IDE.
Last time, we covered the assert method for testing truth. But there are lot more things you 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 up on 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 implements the 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, readable tests as you learn things up. One interesting one is assert_same, which can prove that all values of True are really the same instance. Also, when you write a test that fails by raising an exception, use assert_raise to document that. You should have a test that shows what happens when you divide by zero, and one that shows what happens when you use an uninitialized variable.
Using an IDE
Most of the ‘True Ruby Folk’ believe with all their flinty hearts that you shouldn’t need anything more than a charred and pointed stick to write excellent Ruby. They are probably right. On the other hand, a good IDE can be like a coding video game and make the process so much more colorful and fun. I’ve sure found that JetBrains’ new RubyMine IDE has made my early 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 with it.
And while you’re learning the language, it’s really nice to have some code completion features so that you can figure out the rest of what you’re typing without going mad switching 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 language features and I’m just through the primitives and variable types.
How many tests can you write?