1

We wrote some tests that are necessary, but very slow. So we configured RSpec to exclude them except on Solano, where we set up an ENV variable.

# spec_helper
unless ENV['RUN_ALL_TESTS'] == 'true'
  config.filter_run_excluding :slow
end

That works, but I'm trying to write a rake task we can call to run every test locally by setting that same ENV variable and then running the suite. I'm having trouble figuring out how to trigger RSpec. This is what I've got now:

# all_tests.rake
require 'rspec/core/rake_task'

desc 'Run all tests, even those usually excluded.'
task all_tests: :environment do
  ENV['RUN_ALL_TESTS'] = 'true'
  RSpec::Core::RakeTask.new(:spec)
end

When I run it, it doesn't run any tests.

Most of the stuff I found is for triggering a rake task inside of a test, testing a rake task, or setting up a Rakefile. We're using rspec-rails, our default rake task is already set up.

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
Phillip Longman
  • 2,512
  • 2
  • 11
  • 15
  • sounds like you're after http://stackoverflow.com/questions/577944/how-to-run-rake-tasks-from-within-rake-tasks – Frederick Cheung Apr 13 '16 at 19:09
  • That definitely looks like what I'm after, but I'm still missing something because `Rake::Task['spec'].invoke` and `Rake::Task['default'].invoke` still do nothing. – Phillip Longman Apr 13 '16 at 20:33
  • Have you tried just `RUN_ALL_TESTS=true rake spec` ? – Taryn East Apr 13 '16 at 23:19
  • Thanks, that works. I'd still love to figure out how to call this inside of a rake task, if anyone has any more suggestions. I was able to successfully invoke other rake tasks using the method from the link, but `Rake::Task['spec'].invoke` returns nil, and `Rake::Task['default'].invoke` just returns `"test"`. – Phillip Longman Apr 14 '16 at 15:10

2 Answers2

5

To run RSpec through its rake integration, you need to both define a task and invoke it:

# all_tests.rake
require 'rspec/core/rake_task'

# Define the "spec" task, at task load time rather than inside another task
RSpec::Core::RakeTask.new(:spec)

desc 'Run all tests, even those usually excluded.'
task all_tests: :environment do
  ENV['RUN_ALL_TESTS'] = 'true'
  Rake::Task['spec'].invoke
end

Rake::Task['spec'].invoke did nothing when you tried it because rake turns a task name which is not a name of a defined task but is a file name into a Rake::FileTask, both on the command line and in Rake::Task. You had no 'spec' task defined, but you have a spec directory, so rake spec ran without error and did nothing.

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
1

I had a similar problem where bundle exec rake default would properly create an RSpec::Core::RakeTask, but bundle exec rake spec would instead create a a Rake::FileTask on the spec directory, with with bundle exec rake spec --trace outputting:

** Invoke spec (first_time, not_needed)

It turned out that rspec-rails was in the :test gem group, where it needed (per the docs) to be in both :test and :development.

Interestingly (?), once I did that, gems that had previously only been in the :test group were also available from the specs even when launched with RAILS_ENV=development. I assume that rspec-rails engages in some magic environment shenanigans behind the scenes.

David Moles
  • 48,006
  • 27
  • 136
  • 235
  • (Noting this as an answer here because it's the only place I've seen the `spec` → `FileTask` problem discussed on StackOverflow.) – David Moles May 24 '18 at 23:06