17

I have one app that runs the specs with just rake, but don't know where or how this task is defined. There are no tasks in lib/tasks.

Part of Gemfile:

group :test do
  gem 'capybara'
  gem 'guard-rspec'
  gem 'rspec-rails'
  gem 'database_cleaner'
  gem 'launchy'
  gem 'oauth2'
  gem 'rack_session_access'
  gem 'factory_girl'
  gem 'webmock'
  gem 'selenium-webdriver'
end

RSpec gems:

guard-rspec (4.5.0)
rspec (3.1.0)
rspec-core (3.1.7)
rspec-expectations (3.1.2)
rspec-mocks (3.1.3)
rspec-rails (3.1.0)
rspec-support (3.1.2)

I'm using Rake 10.4.2 and Rails 4.1.6.

Also, when I add:

task :default do
  puts 'No default task.'
end

to the Rakefile, first it runs the specs, and then prints "No default task."

EDIT: Add Rakefile

# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require File.expand_path('../config/application', __FILE__)

Rails.application.load_tasks
B Seven
  • 44,484
  • 66
  • 240
  • 385
  • possible duplicate of [Overriding rails' default rake tasks](http://stackoverflow.com/questions/8112074/overriding-rails-default-rake-tasks) – Jordan Running Mar 05 '15 at 18:35
  • Looks like it could be rspec-rails adding `spec` as a dependency of the default task: https://github.com/rspec/rspec-rails/blob/6d1b893b936e6651fbb96771fefdefff8a7627fe/lib/rspec/rails/tasks/rspec.rake#L6 – matt Mar 05 '15 at 18:44

1 Answers1

29

If you have rspec-rails in your Gemfile, then when the gem is loaded by Rails is will execute this line:

https://github.com/rspec/rspec-rails/blob/v3.2.1/lib/rspec-rails.rb#L19

load "rspec/rails/tasks/rspec.rake"

which in turn defines the default rake task:

https://github.com/rspec/rspec-rails/blob/v3.2.1/lib/rspec/rails/tasks/rspec.rake#L6

task :default => :spec

That's why the default rake task runs the specs in your Rails app.

Furthermore, when you added this code:

task :default do
  puts 'No default task.'
end

That does not in fact redefine the default task: it augments it. The way that Rake task definition works, each declaration of the same task name adds to that task. It does not redefine it. Hence the result that you see "No default task" and the specs are run.

matt
  • 78,533
  • 8
  • 163
  • 197
Matt Brictson
  • 10,904
  • 1
  • 38
  • 43
  • OK, that makes sense. But I have the same gem in another Rails app and get `rake aborted! Don't know how to build task 'default'`. I wonder how to figure out why it doesn't have the rake task. – B Seven Mar 05 '15 at 22:13
  • @BSeven I suggest opening a separate question for that, along with the Gemfile and Rakefile of the Rails app that has the `don't know how to build task 'default'` problem. – Matt Brictson Mar 05 '15 at 23:45
  • Is there any way of removing the `spec` task dependency from the `default` task? – Joshua Pinter Jul 22 '18 at 21:58