89 database tables, 93 fully RESTed objects, 4740 successful tests. Life is grand.I've spent years working in web development with PHP (as I've describe ad naseum), and I'd hate to admit it, but I've never written a test. Ever. My stuff works, but soon after every launch, small cracks start to appear. The worst part is that these small cracks are always found by my users. This is not only embarrassing, but it leads to me furiously debugging a live system with active users, trying to quickly recreate the conditions that caused the error. I like my hair and I've losing it fast enough, thank you.Enter RSpec and Behavior Driven Development (BDD). RSpec makes writing tests a joy. Yes, I know, I know, but testing can be a real joy. It's like working out; it always seems super difficult to start, and you stall and think of a billion other things to do, but once you actually start you feel really good and alive.BDD makes testing second nature, and RSpec makes writing these tests intuative. You know those meetings you have with your clients and you try to distill what they truly need from what they tell you they need? BDD gives you a language that both you and your clients can speak; you simply describe how an object should behave:A Car not only has a color, it also should not be driveable with one tire and it should only be driveable by a licensed driver.
describe "A Car" do before(:each) do @car = mock_model(Car) @driver = mock_model(Driver) end it "should have a color" do @car.color = nil @car.should_not be_valid end it "should not be driveable with one tire" do @car.tires = 1 @car.is_driveable?.should_not be_true end it "should be driveable with four tires" do @car.tires = 4 @car.is_driveable?.should be_true end it "should not be driveable by an unlicensed driver" do @driver.stub!(:license_active?).and_return(false) @car.stub!(:driver).and_return(@driver) @car.is_driveable?.should be_false end it "should be driveable by a licensed driver" do @driver.stub!(:license_active?).and_return(true) @car.stub!(:driver).and_return(@driver) @car.is_driveable?.should be_true endend
See? Simple. As with most Ruby code, RSpec features English like statements that are immediately comprehensible to anyone -- even clients. You know what the best thing is about RSpec though? It not only tests your code, it documents your code. Just look:
Imagine testing and documenting your code at the same time! Two things that many programmers skip altogether! That alone makes the Ruby on Rails framework and mindset the most attractive choice today.A Car should have a color should not be driveable with one tire should be driveable with four tires should not be driveable by an unlicensed driver should be driveable by a licensed driver














I just got into RSpec recently and it really is amazing. I’ve found that my code is actually a lot leaner now then before with Ruby’s Test::Unit.
4000 specs is a pretty nice amount. Hows your test coverage?
Last time I checked it was clocking in around 89% or so, but that was because I had a bunch of migration code in there.