Conveniently run one spec file from Rake
| Published: | Comments: 2 | Filed under: Development
After my frustrating ordeal with Autotest last night, I decided that if Autotest didn't want to cooperate, I at least needed to set up an easier way to manually run specs for a single file.
This rule allows more convenient access to individual spec files, without the hassle of rake spec SPEC_FILE="spec/foo_spec.rb".
require 'spec/rake/spectask'
rule(/spec:.+/) do |t|
name = t.name.gsub("spec:","")
path = File.join( File.dirname(__FILE__),'spec','%s_spec.rb'%name )
if File.exist? path
Spec::Rake::SpecTask.new(name) do |t|
t.spec_files = [path]
end
puts "\nRunning spec/%s_spec.rb"%[name]
Rake::Task[name].invoke
else
puts "File does not exist: %s"%path
end
end
It dynamically creates and invokes a SpecTask depending on the task name you give it. For example, if you run rake spec:color, it will run the specs in spec/color_spec.rb. You can run multiple specs (rake spec:color spec:music spec:surface), or even run specs in a subdirectory of spec (rake spec:foo/bar).
For completeness, I also defined a spec:all task which runs all specs. This is pretty simple, but I'll post it here in case it helps someone:
namespace :spec do
desc "Run all specs"
Spec::Rake::SpecTask.new(:all) do |t|
t.spec_files = FileList['spec/*_spec.rb']
end
end
Comments