Automagical RSpec: Sake Spec for your Scripts 3
I’ve been trying to attempt at spec’ing every single tiny lil script and bit of code I write a long the way. This means I needed a quick, cross directory/app/project script that would run specs from the current directory.
I love Rake, and I love Sake even more.. and Sake was born to do this sort of system-wide task… so lets get to the code!
Throw this in your ~/.sake and smoke it:
desc 'runs specs in the current project, with its own SpecHelper setup'
task 'spec' do
require 'rake'
require 'spec/rake/spectask'
module SpecBase
def self.included(klass)
Object.class_eval do
require 'rubygems'
require 'spec'
Spec::Runner.configure { |config| config.mock_with(:mocha) }
end
end
end
Spec::Rake::SpecTask.new("spec") do |t|
t.spec_opts = ["--format", "specdoc", "--colour"]
t.spec_files = Dir["spec/**/*_spec.rb", "./*_spec.rb"].sort
include SpecBase
end
endAs the description string states, this task encapsulates spec_helper.rb and runs any _spec.rb files in the current directory. This makes it very quick for writing specs for small scripts.
Of course, I try to maintain a convention of naming my spec’s just like in Spec::Rails. So if I’m writing a eat_bacon.rb script, I use eat_bacon_spec.rb as the spec file.
Also note: I’m loading Mocha up there, so make sure to take that out if you don’t need it.
Enjoy, with Sake!
