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!
Comments
-
I have wanted one of these forever! THANKS for the great work
-
Great tip. Is it possible to combine that with autotest? Calling "autotest" calls just "spec YOUR_SPEC_FILES" and not your sake task. I have the problem with autotest that it just gives the correct output if I change the spec-files. Changing the code-files does not give the correct output by autotest. Example ------------------- Directory: CustomerTest customer.rb spec/customer_spec.rb I call "autotest" in "CustomerTest". Output: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -S /Library/Ruby/Gems/1.8/gems/rspec-1.1.3/bin/spec spec/customer_spec.rb ... Finished in 0.005555 seconds 3 examples, 0 failures ------- Do I change the spec file -- Output: [...] 3 examples, 0 failures ------- Do I change the code file -- Output: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -S /Library/Ruby/Gems/1.8/gems/rspec-1.1.3/bin/spec It does not call the spec file Any hints?
-
O my god, what happend to my line breaks?
