Listing posts in Testing
Testing Rake tasks with Cucumber
Posted June 07, 2009 23:28 in Testing, Rails by Andreas
One great thing about Cucumber is that it provides a great entry-point for testing code not directly associated with your application, in example Rake tasks. And since nowadays everyone is
TATFT you should also be testing your Rake tasks.
The challenge
While developing this blog, I wanted to populate the database with some test data, first and foremost to see how the design managed larger amounts of data.
HTFDITT? (How The Fuck Do I Test This?)
First you need to write the feature. It could look something like this.
Then you need to implement the steps. I made a file called rake_task_steps.rb in features/step_definitions.
To make it work you need to require some rake libs, and load the rake-file you are testing. Don’t try to replace the rake step with something like `rake #{task}`, especialy if you’re using sqlite3 as your test database, I spent half a night trying to figure out why my test wasn’t working properly…
Aftermath
When the tests are red, then you just code one step at a time until they are all green. You could debate the granularity of the last step, since it actually passes if there are more than one record in the database, while my rake task generates a couple of hundred. If you would want to populate you own database, I’d recommed using either factory_girl or Machinist. Two great gems for mass production and fixture replacements.