1
votes

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.

2
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
Have you tried just RUN_ALL_TESTS=true rake spec ?Taryn East
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

2 Answers

5
votes

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.

1
votes

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.