[rspec-users] Using RSpec to test Rake tests

John Feminella johnf.pub at distb.net
Mon Apr 26 17:29:28 EDT 2010

hi guys,

I'm trying to be diligent about checking my `rake` tasks with RSpec
tests, but in the process of feeling my way around I seem to have hit
a wall. I've got a really simple RSpec test that looks like this:

   # ./test/meta_spec.rb
   describe "Rake tasks" do
     require 'rake'

     before(:each) do
       @rake = Rake::Application.new
       @rake.load_rakefile  # => Error here!
       Rake.application = @rake

     after(:each) do
       Rake.application = nil

     it "should have at least one RSpec test to execute" do
       Rake.application["specs"].spec_files.size.should > 0

I have a simple task called "specs" defined in `./Rakefile.rb` which
has a Spec::Rake::SpecTask that includes all the `*_spec.rb` files.

If I put the `@rake.load_rakefile` method in (which is my
understanding of what the Rake docs tell you to do when
programmatically instantiating Rake::Application instances), I want
that Rakefile to load. But instead it just bombs out. If I comment it
out, however, the test fails because the "specs" task is
(understandably) not defined.

Where am I going wrong? (I suspect this might be more of a Rake
question than a RSpec one, but I've posted on this list just in case
my assessment is off.) Or is the goal of testing Rake tasks better
served by something other than RSpec?

Any help is appreciated. Thanks very much!

~ jf
John Feminella
Principal Consultant, Distilled Brilliance

More information about the rspec-users mailing list