NullVoxPopuli
NullVoxPopuli

Reputation: 65133

How do I setup rspec with rails 2.3.8 and bundler?

A lot of the guides I've been finding don't use bundler.

this is the part of the gemfile I'm using for tests:

group :test do
    gem "cucumber"
    gem "cucumber-rails"
    gem "launchy"
    gem "hpricot"
    gem "gherkin"

    gem "capybara"
    gem "rspec"
    gem "rack"
    gem "rspec-rails"

    gem "webrat"
    gem "database_cleaner"

    gem "factory_girl"
    gem "shoulda", :require => nil
    gem "shoulda-matchers", :git => "https://github.com/thoughtbot/shoulda-matchers"
    gem "cobravsmongoose"

    gem "rcov"
    gem "ZenTest"
    gem "autotest-growl"
    gem "inherited_resources", "1.0.2"
    gem "responders", "0.4.2"
end

But even with all that, the generators never exist. so doing: script/generate rspec doesn't work, (can't find the rspec) generator

generators would be installed if the gems were installed as plugins... but I think that just adds bloat to the app, and different gems compile differently on different OSes.

So, anyone have any guides for setting up rspec with bundler with rails 2.3.x?

Upvotes: 3

Views: 1926

Answers (4)

Nesha Zoric
Nesha Zoric

Reputation: 6620

In rails 5.1.4 you there are four easy steps to get your RSpec up and running:

group :development, :test do
  gem "database_cleaner"
  gem "rspec-rails"
end
  1. Add the above gems to the :test and :development groups in your Gemfile.

  2. run bundle install from the command line

  3. run rails generate rspec:install from the command line, it will create the following files:

create .rspec create spec create spec/spec_helper.rb create spec/rails_helper.rb

  1. configure spec_helper.rb and rails_helper.rb

You can check more detailed info on: https://kolosek.com/rails-rspec-setup.

Upvotes: 0

Edward Anderson
Edward Anderson

Reputation: 13916

Setting up RSpec, Guard, and Spork on a Rails 2 project

I've done this a few times now; hopefully this will be helpful to anyone needing to maintain Rails 2.3 apps. This has worked great for the apps I've worked on, but I welcome contributions from others who suggest additional steps.

This guide assumes a Rails 2.3.x project on Bundler

  1. Get rid of any old rspec plugins that are in your project, if any. RSpec bits may be hiding in:
    • Rakefile
    • lib/tasks/rspec.rake
    • vendor/plugins/rspec
    • (anything else you can find)
  2. RSpec 2 is not compatible with Rails 2; use RSpec 1 (docs). Put the most recent compatible gem versions to your Gemfile:

    group :test, :development do
      gem 'test-unit', '1.2.3', :require => false # for rspec
      gem 'rspec', '~> 1.2', :require => false
      gem 'rspec-rails', '~> 1.2', :require => false
      gem 'guard', :require => false
      gem 'spork', '~> 0.8.0', :require => false
      gem 'guard-rspec', :require => false
      gem 'guard-spork', :require => false
      gem 'growl', :require => false      # notifications; optional
      gem 'rb-fsevent', :require => false # for OSX; optional
      gem 'listen', '>= 0.5.1', :require => false
      gem 'machinist', '~> 2.0', :require => false
      gem 'database_cleaner', '~> 0.9.1', :require => false
    end
    

    The :require => false options are optional, but it helps the app to start up faster in development if it doesn't have to load testing libraries outside of when SpecHelper.rb requires them.

  3. Install the bundle. Use bundle update for any gems that were already in your Gemfile.

  4. Ensure lib/tasks/rspec.rake and spec/spec_helper.rb do not exist.
  5. script/generate rspec
  6. Remove the config.gem line that was added to config/environments/test.rb; the app uses bundler.
  7. spork --bootstrap

    Then edit spec/spec_helper.rb and follow the instructions.

    Move everything from the stock spec_helper.rb into the prefork block, except:

    Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
    

    belongs in each_run.

  8. Install database_cleaner. In spec/spec_helper.rb,

    In the prefork block:

    require 'database_cleaner'
    

    In the each_run block:

    DatabaseCleaner.clean
    
  9. Initialize Guardfile

    • guard init spork
    • guard init rspec
    • Modify the Guardfile's rspec guard to use the correct version and drb (spork):

      guard 'rspec', :version => 1, :cli => '--drb --color' do
      
    • Modify the Guardfile as appropriate for your project

  10. Run rake spec. You should get no output (if you have no tests). If you get errors, resolve them.

  11. Run guard. No errors? Great, test away!

    Problems? Try again more quickly by running spec spec instead of re-running guard.

Upvotes: 8

nzifnab
nzifnab

Reputation: 16092

We still have an app on rails 2.3.8, but we updated it to use bundler (Gemfile), and it has rspec and cucumber working as well.

Make sure you follow the bundler guide to make your app correctly use the Gemfile's gem loading instead of Rails' default: http://gembundler.com/rails23.html

After you get that preinitializer.rb and change the config/boot.rb working correctly, you might need to make sure you're using the right versions of rspec and cucumber.

I think just that generic gem 'rspec-rails' might try installing rspec 2 for you, but that only works on Rails 3 (I believe), so you might need to specifically tell it to use rspec 1.x.

Our test group looks like this (although I think some of these gems may be older than they need to be, it's been awhile since we've updated them since a rails 3 upgrade for the app is pending we're not too worried about what it looks like right now):

group :test, :cucumber do
  gem 'autotest-fsevent'
  gem 'test-unit', '~>1.2.3'
  gem "hoe", "1.5.1"
  gem 'autotest-rails', '4.1.0'
  gem 'rspec', '1.3.2'
  gem 'rspec-rails', '1.3.4'
  gem 'cucumber', '0.10.0'#, '0.9.0'
  # Change this shinanigans to 0.4.0 when it gets released ;)
  gem 'cucumber-rails', '0.3.2'
  gem 'database_cleaner', '0.5.2'
  gem 'capybara', '0.3.9'
  gem 'launchy'
  gem 'dupe', '0.5.1'
  gem 'factory_girl', '1.2.4'
  gem 'email_spec', '~>0.6.2', :require => false
end

After doing this, and running bundle install, I am able to type the command script/generate --help which includes this in the output:

Installed Generators
  Rubygems: business_time_config, cucumber, culerity, dupe, email_spec, feature, integration_spec, paperclip, rspec, rspec_controller, rspec_model, rspec_scaffold
  Builtin: controller, helper, integration_test, mailer, metal, migration, model, observer, performance_test, plugin, resource, scaffold, session_migration

As you can see, the cucumber and rspec generators are in fact available there.

I think your problem might be the version of rspec it's installing. If it's installing rspec version 2, then that is tied to rails 3, which handles generators in gems differently I believe (I believe they have to be put in a different directory structure). That could be why your rails 2.3.x app isn't seeing them.

You don't have to follow my versions exactly, I'm not a fan (at all) of putting specific versions in a Gemfile but we ended up doing it here way back when because a) we didn't fully understand bundler, and b) We needed to make sure we were getting rails 2.3-compatible gems.

Hopefully this helps! Let me know if you have questions.

Upvotes: 5

nicholaides
nicholaides

Reputation: 19489

The reason the generators don't exist is that when you run rails generate ..., it's executing in the development environment while these gems are only loaded in the test environment.

Option 1

Add them to both development and test environments.

Option 2

Run rails generate ... RAILS_ENV=test

(I'm not positive that this option will work.)

Upvotes: 0

Related Questions