Screencast of Test Driven Development with Ruby: Part 2 – Exceptions

Update: Distributing media can be problematic. If you’ve read the comments below, I’m sure you agree that I’ve had my fair share of difficulties. There have been two issues that I’ve been struggling with recently; (a) the format that I was using (avi’s) only worked on some machines; and (b) the files were not always accessible.

To address the first problem, I’ve converted the orginal avi’s to mp4’s. I’ve tested the mp4’s under VLC and they play without any problems. The original avi’s still work with Windoze Media Player. The bad news is that neither format works with Quicktime. I’m inexperienced with QT and I’m out of options at this point. I’m open to suggestions on how to resolve this, if you have any.

To address the second problem, I’ve moved the files to a new (hopefully more reliable) host. The host also offers torrents, and that’s available as well. So, here are the screencasts:

Individual avi files:

  1. Intro to TDD (Session 1) [link]
  2. TDD with Ruby (Session 2) Part 1 of 4 [link]
  3. TDD with Ruby (Session 2) Part 1 of 4 [link]
  4. TDD with Ruby (Session 2) Part 1 of 4 [link]
  5. TDD with Ruby (Session 2) Part 1 of 4 [link]

Individual mp4 files:

  1. Intro to TDD (Session 1) [link]
  2. TDD with Ruby (Session 2) Part 1 of 4 [link]
  3. TDD with Ruby (Session 2) Part 1 of 4 [link]
  4. TDD with Ruby (Session 2) Part 1 of 4 [link]
  5. TDD with Ruby (Session 2) Part 1 of 4 [link]

Now, back to the original article …

My previous screencast [1] was a simple introduction to TDD with Ruby. The intention was to demonstrate TDD and not much else. Having done that there are a number of other topics that I feel need to be addressed including exceptions, database interaction and mocks.

In this screencast I’ve focused on TDD with exceptions and error handling.

Exceptions and error handling
The approach to testing exceptions and error handling is really no different that standard TDD approach, which is outlined below:

  1. Write the test.
  2. Run the test and watch it fail.
  3. Write the code.
  4. Run the automated tests, get the code to pass the test.
  5. Refactor.

The only real differences are the unit testing asserts that are used. Thankfully the unit testing framework in Ruby includes a number of asserts that are specifically designed to easly check for exception and error conditions. In particular the Ruby unit testing framework defines the following asserts;

assert_raise(*args) {|| …} Passes if block raises one of the given exceptions.
assert_nothing_raised(*args) {|| …} Passes if block does not raise an exception.

There is also assert_throws and assert_nothing_thrown. The way that Ruby handles thrown exceptions is a rather interesting. You associate a symbol with the thrown exception and the stack is unwound to that point. It’s a bit like a cross between an exception and a goto.

I don’t have any really experience using throw, so all my exceptions tend to be very Java-ish and I only ever raise exceptions. I’m sure this will change in time as I become more experienced with Ruby.

Implications of TDD
As you watch the screencasts, you will probably notice that TDD influences the way that I write code. I typically work in very small, fine grained sections of code. And, because I’m working with small sections of code, I’m frequently saving my work. I’m not working with a source repository (CVS, SVN etc) but if I were, I would be frequently checking-in my code.

Using TDD means that I need to be able to quickly execute the test casts. In a compiled language such as Java or C#, I would need to be able to quickly compile and execute the test cases. If my environment is structure such that it takes 20 minutes to compile and test 10 lines of code, then clearly TDD is impractical.

Half the challenge of TDD is promoting a componentized compilation and test framework where unit tests can be quickly run.

What’s not included
This screencast is intended to demonstrate a more realistic example of TDD (i.e. more complex than the simple case presented last time) with exceptions. I have not tried to address the following:

  • databases and database related issues
  • mocks

The Screencasts
When I last did the screencast, I recorded the session in several different parts and combined them all with Windows Movie Maker. As you would expect, the quality of the resulting file was pretty poor. This time I haven’t tried to combine them, so the you can experience them in their naked glory!

Bookmark and Share

[1] Screencast of Test Driven Development with Ruby: Part 1 – A Simple Example
[2] Test Driven Development, by Kent Beck
[3] Test Driven Development in Microsoft .Net, by James W. Newkirk and Alexei A. Vorontsov

6 thoughts on “Screencast of Test Driven Development with Ruby: Part 2 – Exceptions

  1. Thank you for this series of screen-casts. I learned something from each one. Your pace and level of detail allowed me to type right along with your examples — something I’d recommend for anyone who watches the videos.

    I was not able to play the mp4s with WinAmp — that’s probably a codec issue — but I had good success with the VLC Media Player (

    Thanks again. I look forward to seeing more of your tutorials in the future.



Comments are closed.