Using RSpec to test Rake tests

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
 end

 after(:each) do
   Rake.application = nil
 end

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

end

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 F.
Principal Consultant, Distilled Brilliance

On Apr 26, 2010, at 4:29 PM, John F. wrote:

 before(:each) do
   Rake.application["specs"].spec_files.size.should > 0
 end

end

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

This is what the default spec task does, and it is well specified in
rspec’s own specs. There is a general rule of thumb that says “test your
code, not everybody else’s.” This suggests that you don’t really need to
be testing this. Any reason you feel the need to?

David

There is a general rule of thumb that says “test your code,
not everybody else’s.” This suggests that you don’t really
need to be testing this. Any reason you feel the need to?

The “specs” task is generated programmatically based on several
initial conditions, and eventually it will become more complex and
selective about (for instance) what it will put in the FileList[…]
to examine.

My understanding is that this isn’t so much checking that Rake
successfully made the task, but rather that the conditional logic
which generates that task is sound. Since that exists on my side, not
Rake’s, I thought it was appropriate to test.

In your view, is that an abuse of RSpec/Rake, or is this a useful thing
to test?

John F.
Principal Consultant, Distilled Brilliance

On Apr 27, 2010, at 8:42 AM, John F. wrote:

successfully made the task, but rather that the conditional logic
which generates that task is sound. Since that exists on my side, not
Rake’s, I thought it was appropriate to test.

In your view, is that an abuse of RSpec/Rake, or is this a useful thing to test?

Given that you’re generating it programmatically it seems reasonable to
me. More below …

describe “Rake tasks” do
end

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

end

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 this were me, I’d drive out an object with a method that determines
the file list and then just use it in the rake task.

class SpecFileFilter
def files

end
end

Spec::Rake::SpecTask.new(‘specs’) do |t|
t.spec_files = SpecFileFilter.new.files
]

There is little need to dig into Rake’s plumbing for this purpose, and
the only risk you bear is that somebody updates the rake task to stop
using the SpecFileFilter object. Make sense?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs