10
votes

Right now, I'm using bundler to manage my gems. Bundler loads different gems for different environments.

I have some rake tasks that use testing gems (rspec), but these cause problems in production environments where that gem isn't loaded.

So what I'd like to be able to do is to only have the rake task (and the require 'rspec/core/rake_task' line associated with it) load in the test environment.

I can't quite figure out the best way to do this.

I currently have:

require "bundler"
require 'rspec/core/rake_task'

desc "Task for running Rspec tests"
RSpec::Rake::SpecTask.new(:spec)
2

2 Answers

12
votes

How about:

require "bundler"

unless Rails.env.production?
  require 'rspec/core/rake_task'

  desc "Task for running Rspec tests"
  RSpec::Rake::SpecTask.new(:spec)
end

Not the prettiest solution, but it will work.

0
votes

A possible solution, perhaps not ideal for all setups, is to add the rake task to your gitignore.