Test Rake Tasks Like a BOSS #ruby #testing

jdclayton October 26, 2011 rails rake rspec testing Testing Rake tasks is one of the most painful things I do as a Ruby developer. Even after extracting all the code out into a separate class (which helps a lot), I still want to make sure I test that the right classes got called correctly with the right arguments. I wanted the subject to be the task, where I could call invoke , check its prerequisites, etc. describe "cron:hourly" do its(:prerequisites) { should include("reports:users") } end describe "reports:users" do before { ReportGenerator.stubs(:generate) } its(:prerequisites) { should include("environment") } it "generates the report" do subject.invoke ReportGenerator.should have_received(:generate).with() end end RSpec has shared contexts, so I set off to find an easy, straightforward way to test Rake tasks. # spec/support/shared_contexts/rake.rb require "rake" shar...