How do I setup rspec with rails 2.3.8 and bundler?
A lot of the tutorials I've found 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
Even so, generators never exist. so: script / generate rspec doesn't work, (can't find rspec generator)
Generatorswill be installed if gems were installed as plugins ... but I think this just adds bloat to the application and different gems compile differently on different OSs.
So, who has any guides for setting up rspec with a 2.3.x rails bundle?
We still have an app for rails 2.3.8, but we updated it to use bundler (Gemfile) and it also works with rspec and cucumber.
Make sure you follow the vendor guide so that your application correctly uses the Gemfile gem download instead of the default Rails: http://gembundler.com/rails23.html p>
After getting this preinitializer.rb and config / boot.rb working properly, you may need to make sure you are using the correct rspec and cucumber versions.
I think only a generic one gem 'rspec-rails'
might try to install rspec 2 for you, but that only works on Rails 3 (I suppose), so you might need to specifically tell him to use rspec 1.x.
Our test group looks like this (although I think some of these gems may be older than they should be, it has been a while since we updated them as a rails 3 update for the app expects us to not too worried about how it looks 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 that and running bundle install
, I can enter a command script/generate --help
that 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, cucumber and rspec generators are actually available there.
I think your problem might be the version of rspec it installs. If it installs rspec version 2 then it has to do with rails 3 which handles generators in gems differently (I believe they need to be placed in a different directory structure). This may be why your rails 2.3.x application is not seeing them.
You don't have to follow my versions exactly, I'm not a fan of putting certain versions in the Gemfile at all, but we ended up doing it back here because: a) we didn't fully understand the bundler and b) we needed to make sure we were getting rails 2.3- compatible gems.
Hope this helps! Let me know if you have any questions.
Configuring RSpec, Guard, and Spork in a Rails 2 Project
I have done this several times; hopefully this will be useful for anyone who needs to maintain Rails 2.3 applications. This worked great for the apps I've been working on, but I welcome contributions from others who suggest additional steps.
This tutorial assumes a Rails 2.3.x Bundler project
- Get rid of any old rspec plugins you have in your project, if any. RSpec bits can be hidden in:
- Rakefile
- Library / tasks / rspec.rake
- vendor / plugins / rspec
- (whatever you can find)
-
RSpec 2 is not compatible with Rails 2; use RSpec 1 ( docs ). Place the most recent compatible gem versions in 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 parameters
:require => false
are optional, but they help the application start up faster in development if it does not need to load test libraries outside the limits required by SpecHelper.rb. -
Install the package. Use
bundle update
for any gems that were already in your Gemfile. - Make sure lib / tasks / rspec.rake and spec / spec_helper.rb do not exist.
-
script/generate rspec
- Remove the line
config.gem
added to config / environment / test.rb; the app uses bundler. -
spork --bootstrap
Then edit spec / spec_helper.rb and follow the instructions.
Move everything from the spec_helper.rb stock to the block
prefork
except:Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
belongs to
each_run
. -
Install database_cleaner. In spec / spec_helper.rb,
In the block
prefork
:require 'database_cleaner'
In the block
each_run
:DatabaseCleaner.clean
-
Initialize Guardfile
-
guard init spork
-
guard init rspec
-
Modify Guardfs rspec protection to use correct version and drb (spork):
guard 'rspec', :version => 1, :cli => '--drb --color' do
-
Change the Security File to match your project
-
-
Run
rake spec
. You shouldn't get a result (unless you have tests). If you get errors, resolve them. -
Run
guard
. No mistakes? Great, check it out!Problems? Try again faster by
spec spec
starting protection instead of restarting.
The reason generators don't exist is because rails generate ...
it runs in the environment when it starts up development
, while those gems are only loaded in the environment test
.
Option 1
Add them to environments development
and test
.
Option 2
Run rails generate ... RAILS_ENV=test
(I'm not sure if this setting will work.)
In rails 5.1.4, you can follow four simple steps to get your RSpec up and running:
group :development, :test do
gem "database_cleaner"
gem "rspec-rails"
end
-
Add the above gems to the: test and: development groups in your Gemfile.
-
run
bundle install
from command line - run
rails generate rspec:install
from command line, it will create the following files:
create .rspec create spec create spec / spec_helper.rb create spec / rails_helper.rb
- tweak spec_helper.rb and rails_helper.rb
You can check more details: https://kolosek.com/rails-rspec-setup .