Friday, July 11, 2008

A stupid reason I like shoulda

I've been interested shoulda for a while but hadn't played around with it until today. I really like the idea of seperate contexts and not having to name methods stuff like "test_process_should_create_record_if_one_doesnt_exist". With some playing around though, I found out that shoulda helps me work around a bug I found in flexmock and rails where the teardown method wasn't getting called correctly in my tests.

As an addendum to that mailing list post, I get this output:

Loaded suite test/unit/test_shoulda_test
Started
inside setup
inside test
inside teardown
.
Finished in 0.069689 seconds.

1 tests, 1 assertions, 0 failures, 0 errors

... when I run this code:

require File.join( File.dirname(__FILE__), '..', 'test_helper' )
require 'test/unit'
require 'flexmock/test_unit'

class TestTest < Test::Unit::TestCase
context "This test" do
setup do
$stderr.puts "inside setup"
end

teardown do
$stderr.puts "inside teardown"
end

should "pass" do
$stderr.puts "inside test"
assert true
end
end
end

Thanks shoulda!

Wednesday, July 9, 2008

Testing Tidbts #1: Be explicit

I was recently refactoring some tests (you do refactor your tests, right? hmm, sounds like an idea for another post..) and I came across something like this, trying to test whether a time field on a rails model was updated correctly:

assert_in_delta mq.ended_at,
Time.now,
1

What's that 1 for at the end? What's the third parameter for assert_in_delta, anyways?

If I had been more explicit, that might be a little more apparent:

assert_in_delta mq.ended_at,
Time.now,
1.second

It's a small change, and totally uneccessary in real, working code, but I feel like it improves the readability a great deal.

Monday, July 7, 2008

Awesome: Easy Join Table Conditions

Most of the new-in-edge-rails features elicit a yawn from me (at best), but this one caught my eye. Some of the projects at work that I assist on could be cleaned up a great deal with this functionality.