-33.702635,151.099434 : Hornsby
September 17th, 2007
So what does a north western Sydney suburb have to do with rails? Not very much, but it has lent it’s name to a very very cool app written by Lachie Cox.
The main aim of Hornsby is to get rid of the dreaded rails fixtures. As a newcomer to Rails about a year ago, testing was a new and exciting thing for me. It took a bit of discipline but I got into it. Then I got out of it. Fixtures where p*ssing me off, badly. They were a nightmare to maintain and actually made me write worse code than normal due to the fact I didn’t want to rewrite all my fixtures.
I then started using Rspec, I love writing specs but fixtures were still making me swear at my MBP.
Hornsby makes me an extremely happy Rails programmer. Here is Lachie’s description.
Here’s my code. It’s an example of setting up a “scenario” that I used for testing my Equipment
1 2 3 4 5 6 |
scenario :equipment do @manufacturer = EquipmentManufacturer.create!(:name => "Ben Hogan") @category = EquipmentCategory.create!(:title =>"Fairway Woods") @equipment = EquipmentProduct.new(:name => "Edge CFT Ti Hybrid",:equipment_manufacturer_id => @manufacturer.id,:equipment_category_id => @category.id) @equipment.save(false) end |
Here is the bit from Rspec test that loads it up.
1 2 3 4 5 6 7 |
describe EquipmentController do before(:each) do hornsby_scenario :equipment end ... end |
You’ll notice I hand in “false” to the save method, this allows us to bypass all the validations and setup just the bare minimum to pass tests. This is helpful sometimes but be aware it can lead to other weird stuff than you might need to be aware of, assuming your validations are setup for a reason. Another nice side effect of Hornsby scenarios is the fact that if your DB tables are not the rails default ones you don’t have to jump though hoops to get your Db populated.
This software has made my testing life a joy yet again, knowing the exact state of your DB when testing is precisely what was missing from fixtures.
Here’s those links again:
Sorry, comments are closed for this article.