Nullcreations.net | Ramblings & Photography of Jerrett Taylor

Changing rake :default to use RSpec

May 30th, 2007 | 1 comment

I dug around a little before digging into the Rake RDoc (which is great!), but I didn't find anything on the intertubes about this so I figured I'd post here in case somebody else is looking.

Hijacking the default Rails rake tasks is actually pretty easy, but if you just try to redefine it by adding a new task :default it will add to the existing tasks. This is by design, so you can add to existing tasks. So, if you just redefine it and add :spec it will kind of work, but it'll run your tests first.

To solve this all you have to do is clear the existing prerequisite (which runs :test).

Put this in a file called default.rake in the lib/tasks directory of your rails project


Rake::Task[:default].prerequisites.clear

That's it! it'll now run your specs instead of tests. Taking it a little bit further, you can use the following as a default.rake instead:

1
2
3
4
5
6
7
8
9
10
11
12
rspec_base = File.expand_path("#{RAILS_ROOT}/vendor/plugins/rspec/lib")
$LOAD_PATH.unshift(rspec_base) if File.exist?(rspec_base)
require 'spec/rake/spectask'
Rake::Task[:default].prerequisites.clear

desc "Run specs with rcov output"
Spec::Rake::SpecTask.new(:default) do |t|
  t.spec_opts = ['--options', "#{RAILS_ROOT}/spec/spec.opts"]
  t.spec_files = FileList['spec/**/*_spec.rb']
  t.rcov = true
  t.rcov_opts = ['--exclude', 'spec,/usr/lib/ruby', '--rails', '--text-report']
end

This will do the same thing, but at the end it will give you a nice RCov spec coverage report. The catch with this is that RSpec will now run twice as - to fix that, you have to open up plugins/rspec_on_rails/tasks/rspec.rake and comment out line #14 ( #task :default => :spec )

This is because Rails loads plugins after it loads lib, so the tasks in rspec_on_rails overide your custom tasks.

1 Comment

Posted by josh2 4 months ago.

Thanks a lot, that approach worked a lot better and solved my problems.

Musings

04.14.08

Trying to decide where to travel to? Wikitravel.org Random Page has the answers!

02.23.08

sd's embedded_actions has a new home on github

02.18.08

Very cool.

02.17.08

Open Source Food? - looks like a nice recipe site!

02.15.08

For anyone using capistrano with more than one target for deployment (staging, production), check out cap multistage

02.15.08

Wow, I just realized how bad my code blocks break my blog in safari. I should fix that!

01.27.08

Mike has created another nice netbeans theme - go check it out, if you use netbeans!

01.21.08

A handy tool, Rubular is a web based regex editor

01.15.08

Dan put together a sweet code editor plugin for mce with syntax highlighting etc.

12.13.07

On OSX? Add RubyImporter to Spotlight and search your source!

12.04.07

sd has released a new version of embedded_actions

11.27.07

RejectConf videos are online thanks to confreaks!

11.19.07

Good news - we upgraded a project at work to rails 2.0 rc2 and the speed to run specs dropped from 30-35 seconds to 10-15 seconds!

11.08.07

Eigenclass.org has a great big list of Ruby 1.9 changes that is being kept up to date!

11.06.07

Rein is working on a trac replacement in ruby called eskort , and it looks cool. More info on his goals