Moving back to Test::Unit?
I’ve been testing my applications and plugins with rspec for a while now. It really is a great testing framework. But occasionally I feel like it’s too complex. And in a way, I almost find its English-like syntax harder to parse than a plain old assertion.
Call me crazy, if you want to.
Really, the piece that I love about writing specs is how you don’t really write methods. You explain what the spec is testing with a string, and execute a block to match that behavior.
1 2 3 4 5 | it "should generate a token upon creating a valid user" do @user.token.should be_nil @user.save @user.token.should_not be_nil end |
That’s a nice API. It sure does beat those long test_* method names you end up with otherwise. And honestly, that (along with some desire to try out the latest and greatest) was my main reason for making the switch.
But now things are a bit different. Jay Fields introduced a similar pattern for Test::Unit, which now ships with Rails.
1 2 3 4 5 6 7 8 9 10 11 12 | def test(name, &block) test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym defined = instance_method(test_name) rescue false raise "#{test_name} is already defined in #{self}" if defined if block_given? define_method(test_name, &block) else define_method(test_name) do flunk "No implementation provided for #{name}" end end end |
See there? Now that piece that I love about writing specs can be done with Test::Unit.
1 2 3 4 5 | test "should generate a token upon creating a valid user" do assert_nil user.token user.save assert_not_nil user.token end |
That’s about as straight-forward as it gets. Which, arguably, is what testing is all about. No need to complicate things. Granted, it’s more of a functional style than the Ruby-esque method chaining that rspec provides, but it still gets the job done. The sheer simplicity and explicitness of an assertion is starting to reel me back in.
I think the next project I work on that requires testing (yeah, yeah, they all should), I’m going to revisit Test::Unit. We’ll see how it goes.

Chris Tuesday, 16 Dec, 2008 Posted at 09:14PM
There seems to be general consensus that testing frameworks are getting too bloated, and I have to agree. I do enjoy rspec’s sugary syntax, but sometimes it just gets in the way. The friendly test method names are what initially drew me to rspec as well, so I’m glad to see that Rails is incorporating that aspect.
I too am planning on going back to vanilla Test::Unit, because
Also, trying to keep an application on edge Rails with edge rspec is self-mutilation. I miss the good old days of being able to easily run autotest on my apps.
John Nunemaker Wednesday, 17 Dec, 2008 Posted at 12:13AM
I’m swinging back towards test/unit as well. Jeremy McAnally just released a few really nice gems (context, matchy, stump) that enhance the stock test/unit and I wrote a bit about them.
They are really light and make test/unit “feel” like RSpec. Best part is you can mix and match. Since you don’t like the matchers of RSpec, you could just use context and stump. Just a thought.
Ryan Wednesday, 17 Dec, 2008 Posted at 08:08AM
@John -
Yeah, I read about those gems recently. I’m just now “officially” going back to
Test::Unit, so it will probably take a little while for me to find a happy medium. That said, I’m going to use it initially without any additions (well, with the exception of thetest '...' doextension).Much of what got old with RSpec (for me) was inside of the actual tests. I began to miss the simplicity of assertions. The fact that context provides the
beforeandaftersyntax is nice. Plus thecontextblock itself. So I can see that becoming a part of my work flow sooner than later.Anyway, thanks for the tip.
Jon Roberts Friday, 02 Jan, 2009 Posted at 06:51PM
Very interested to read this. I am returning to Rails and worry that its becoming gratuitously complicated, just like Java did.
Anyway I was going to learn about rspec but will try this first.
Thanks.