Beginner’s Perspective on Rspec and Test::Unit

The other day Aaron Patterson asked whether it was OK to teach rspec vs Test::Unit for Ruby beginners.

My own experience was that rspec was really confusing at first: it appears to have a lot of magic. The hard part was understanding where methods and matchers came from. I Don’t like magic, I like to understand how things work.

I was familiar with other unit testing frameworks where you inherit from a base class and got access to the assertion functions via inheritance. That made complete sense to me.

But rspec is different: there’s no explicit class definition, and methods that aren’t monkey-patched onto objects appear out of nowhere. And how come I can use a before(:each) block to write to a @ prefixed variable? Isn’t that an instance variable? But I’m not inside a class!

I did, however, prefer the tree-like structure of nested test cases and liked how the code read as a description of behavior rather than a list of assertions.

Attempting to write my own DSL a few months later helped me piece together a rough understanding of how all that magic works.

Hint: Its not actually magic.