Ssetting up rspec2 task in Rakefile

2019-04-24 13:11发布

问题:

I have a Rakefile that looks like this:

require 'rspec/core/rake_task'

desc "Run all RSpec tests"
RSpec::Core::RakeTask.new(:spec)

This isn't working though. For example, if I try to run "rake -T", I get:

code/projects/bellybuster[master]% rake -T --trace
(in /Users/craig/code/projects/bellybuster)
rake aborted!
no such file to load -- rspec/core/rake_task
/Users/craig/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
/Users/craig/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
/Users/craig/code/projects/bellybuster/Rakefile:1:in `<top (required)>'
/Users/craig/.rvm/gems/ruby-1.9.2-p180/gems/rake-0.8.7/lib/rake.rb:2383:in `load'

Any thoughts?

In case it might be helpful here's the Gemfile:

source :rubygems

gemspec

Oh and some versions:

  • Ruby: 1.9.2p180
  • Rake: 0.8.7
  • Bundler: 1.0.13
  • RubyGems: 1.7.2

回答1:

The syntax looks fine to me. Are you 100% sure you have rspec 2 installed? Does it appear with gem which rspec? Maybe you forgot to run bundle install or you don't list rspec in the .gemspec file as a (development) dependency?



回答2:

Are you using Heroku?

I got the same problem, found this solution at The Fancy Manual:

## One common example using the RSpec tasks in your Rakefile. 
## If you see this in your Heroku deploy:

$ heroku run rake -T
Running `bundle exec rake -T` attached to terminal... up, ps.3
rake aborted!
no such file to load -- rspec/core/rake_task

## Now you can fix it by making these Rake tasks conditional 
## on the gem load. For example:

## Rakefile

begin
  require "rspec/core/rake_task"

  desc "Run all examples"
  RSpec::Core::RakeTask.new(:spec) do |t|
    t.rspec_opts = %w[--color]
    t.pattern = 'spec/*_spec.rb'
  end
rescue LoadError
end

## Confirm it works locally, then push to Heroku.


回答3:

Are you using Travis-CI? I fixed it by moving 'rake' from the gemspec to the Gemfile, i.e.:

source "https://rubygems.org"

# Specify your gem's dependencies in pipboy.gemspec
gemspec

group :test do
  gem 'rake'
end

Not sure if it is the correct solution, but it worked for me..



回答4:

I just nuked my existing gem folder and re-installed everything by running bundle install. That solved the problem for me.