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?
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
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)
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.
Install the bundle. Use bundle update for any gems that were already in your Gemfile.
Ensure lib/tasks/rspec.rake and spec/spec_helper.rb do not exist.
script/generate rspec
Remove the config.gem line that was added to config/environments/test.rb; the app uses bundler.
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.
Install database_cleaner. In spec/spec_helper.rb,
In the prefork block:
require 'database_cleaner'
In the each_run block:
DatabaseCleaner.clean
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
Run rake spec. You should get no output (if you have no tests). If you get errors, resolve them.
Run guard. No errors? Great, test away!
Problems? Try again more quickly by running spec spec instead of re-running guard.
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.
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.)
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
Add the above gems to the :test and :development groups in your Gemfile.
run bundle install from the command line
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
configure spec_helper.rb and rails_helper.rb
You can check more detailed info on: https://kolosek.com/rails-rspec-setup.
Related
I am trying to set up my Rails Angular project to provide JS tests. I have tried almost everything that I have found on Google:
Karma (formerly Testacular)
Jasmine + Jasmine-headless-WebKit
Jasminerice
some other tutorials
but I failed with all of them. I'm looking for a way to run unit and e2e tests in the most painless way (it can be in Guard or Karma, I don't care, but it must run it automatically in the background).
Does anyone have some nice article with a nice example of how to achieve this? In my research I have found this, but IMHO it is an example of how to NOT do this.
My actual Gemfile:
source 'https://rubygems.org'
# Use Ruby 1.9.3 instead default Heroku's 1.9.2
# for development I suggest https://gist.github.com/1688857
ruby '1.9.3'
gem 'rails', '3.2.12'
# Use PostgreSQL, which is quite awesome, fast and easy
gem 'pg'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'bootstrap-sass', '~> 2.3.1'
# I heard that you like Compass
gem 'compass'
# Angular.js
gem 'angularjs-rails'
gem 'angularjs-rails-resource'
gem 'angular-ui-rails'
# Assets should be minified before production
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
# Serve static pages like a boss
gem 'high_voltage'
# Some user management will be nice
gem 'devise' # User management
# gem 'cancan' # And they privileges
# To use Jbuilder templates for JSON
gem 'jbuilder'
# Be fast and deadly as Puma
#gem 'puma'
# We need also some administration panel, don't we?
gem 'rails_admin'
# Some helpers
gem 'andand'
group :development do
# IRb is ugly. Use Pry for the God's sake
gem 'pry-rails'
# Deploy with Capistrano
# gem 'capistrano'
# or Vlad the Deployer
# gem 'vlad'
# Why bother yourself with rerunning tests? Use Guard
gem 'guard'
gem 'guard-rspec'
gem 'guard-spork'
gem 'guard-livereload'
gem 'guard-jasmine'
gem 'rb-fsevent', require: false
gem 'rb-inotify', require: false
# Who like ugly error pages? We don't.
gem 'better_errors'
gem 'binding_of_caller'
# Prettier documentation
gem 'yard'
end
group :development, :test do
# Use RSpec for testing
gem 'rspec-rails', '~> 2.12.0'
# Test JS using Jasmine
gem 'jasmine'
gem 'jasmine-headless-webkit'
# Some DB table generator
gem 'factory_girl_rails', '~> 4.1.0'
# And fake data generator
gem 'ffaker'
end
group :test do
# Some Gherkins will be also good (to vodka of course)
gem 'turnip', '~> 1.1.0'
# Aww, an of course some web browser will be also apprised
gem 'capybara', '~> 2.0.1'
# Clean DB after tests
gem 'database_cleaner'
# Some nice matchers
gem 'shoulda-matchers'
# Extend your mocks
gem 'bourne', '~> 1.2.1'
# Coverage reports will be nice
gem 'simplecov', require: false
end
PS:
It would be nice if I can create coverage reports in a simple way.
You can use teaspoon it runs your favorite test suite (default is jasmine) and they have a wiki to integrate with angular. Teaspoon integrates with the asset pipeline, so you can throw all the angular gems in there and require them in the teaspoon generated spec_helper.js.
The other cool thing is: they have a guard plugin available as well.
I feel your pain, it took me a bit of trial and error to get all tests running for my Rails app. I ended up using Guard for developer continuous testing for specs, acceptance, and javascripts. Here are the fruits of my labor.
This is the Gemfile snippet you will need to include. It has the required Gems for Guard, Jasmine, Rspec, and Turnip. This setup will work with MRI and JRuby, we run our tests on both.
group :test, :development do
# Guard
gem 'guard-jasmine'
gem "guard-bundler", ">= 1.0.0"
gem "guard-rails", ">= 0.4.0"
gem "guard-rspec", ">= 2.5.2"
gem "rb-inotify", ">= 0.9.0", :require => false
gem "rb-fsevent", ">= 0.9.3", :require => false
gem "rb-fchange", ">= 0.0.6", :require => false
# Rspec
gem "rspec-rails", '~> 2.12.2'
# Jasmine
gem "jasmine", '~> 1.3.1'
gem "jasminerice"
end
group :test do
# Turnip
gem "turnip", '~> 1.1.0'
gem "capybara", '~> 2.0.3'
end
This the full Guardfile that watches Rspec, Turnip, and Jasmine to trigger tests:
guard 'rspec' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
# Rails
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }
# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
end
guard :jasmine do
watch(%r{spec/javascripts/spec\.(js\.coffee|js|coffee)$}) { 'spec/javascripts' }
watch(%r{spec/javascripts/.+_spec\.(js\.coffee|js|coffee)$})
watch(%r{spec/javascripts/fixtures/.+$})
watch(%r{app/assets/javascripts/(.+?)\.(js\.coffee|js|coffee)(?:\.\w+)*$}) { |m| "spec/javascripts/#{ m[1] }_spec.#{ m[2] }" }
end
Now things get a little tricky. To get Jasmine to properly load up AngularJS for tests I downloaded angular-mocks.js 1.0.7 to the directory spec/javascripts/support/angular-mocks.js. This is used by the spec helper and the jasmine config to run the AnguljarJS specs.
For the spec/javascripts/spec_helper.js.coffee, point it at the Rails app javascript, the angular-mocks.js, and all the javascript specs:
#=require ../../app/assets/javascripts/application
#=require ./support/angular-mocks
#=require_tree ./
For the Jasmine config, spec/javascripts/support/jasmine.yml, the config will need to point at the Rails app javascript and the angular-mocks.js. Here is the we use, but with the comments removed for brevity:
src_files:
- assets/application.js
- spec/javascripts/support/angular-mocks.js
stylesheets:
- stylesheets/**/*.css
helpers:
- helpers/**/*.js
spec_files:
- '**/*[sS]pec.js'
src_dir:
spec_dir: spec/javascripts
With everything set up, you simply have to run bundle exec guard and all the tests will run and be triggered by development changes.
I've written a blog post about a very simple way to set up AngularJS unit testing on a Rails app using the default Jasmine gem. It took a small code change to get things working smoothly, which has been accepted by the Jasmine team.
http://pivotallabs.com/unit-testing-angularjs-using-jasmine/
In an nutshell:
Add jasmine and angularjs-rails to your Gemfile
gem "angularjs-rails"
gem "jasmine", github: "pivotal/jasmine-gem"
Install the gems and run the Jasmine generator
$ bundle install
$ rails g jasmine:install
Add angular to your application.js, before the rest of your application code
//= require angular
//= require_tree .
Create a file named ./spec/javascripts/helpers/angular_helpers.js and add this line
//= require angular-mocks
Now you can include AngularJS implementation files in your app/assets directory and write tests that use the provided mocks. And when you upgrade the angularjs-rails gem, you will get new versions of angular.js and angular-mocks.js at the same time.
You may have some luck checking out the angular boilerplate project it uses grunt as the build system which I realise is not ruby in anyway, however I think when trying to test things with angular your only hope is to go the nodejs/karma route.
This setup watches for changes in your code and runs the tests. However in my limited experience I haven't been able to get it to show the results of the test run. I am not sure whether I am doing something wrong or if it is not supported.
Anyway hope this helps
I'm using the "ruby on rails by example tutorial" (screencasts) by Michael Hartl and I'm getting some errors during the third lesson (sample app) while trying to do "bundle install".
I changed the Gemfile as shown in the tutorial, as shown in the website(the updated one), and even tried the final Gemfile for this tutorial. Every time I get a different error that something couldn't been install, and the bundle installation could not continue.
At first it said it about 'nokogiri', then 'json', and now 'bcrypt'. This did not happen when I did the first app and the demo app. maybe because now I tried added the rspec? I don't want to continue the tutorial without adding it to the Gemfile, because it sounds important.
I'm running osx lion 10.7.2, rails version 3.0.1.
Copying Gemfile code from comment into original post:
source 'rubygems.org';
gem 'rails', '3.0.1'
gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3'
group :development do
gem 'rspec-rails', '2.0.0.beta.18'
end
group :test do
gem 'rspec', '2.0.0.beta.18'
end
update: I heard from someone that rspec is a gem used on tests, therefore you can't make a rails project without the default test and then change the gemfile. So why in the tutorial he make a "rails new sample_app -T" but afterwords changes the Gemfile so it uses rspec? he says, that they replace each other. that rspec replace the original test, and therefore you need to make a project without the original test. any thoughts on this?
I screwed around with this error for a few hours, then checked the rspec github page. Per their instructions, you need to include the path to github. So I made by Gemfile look like following and it now works:
group :development do
gem "rspec-rails", :git => "git://github.com/rspec/rspec-rails.git"
gem "rspec", :git => "git://github.com/rspec/rspec.git"
gem "rspec-core", :git => "git://github.com/rspec/rspec-core.git"
gem "rspec-expectations", :git => "git://github.com/rspec/rspec-expectations.git"
gem "rspec-mocks", :git => "git://github.com/rspec/rspec-mocks.git"
end
group :test do
gem "rspec-rails", :git => "git://github.com/rspec/rspec-rails.git"
gem "rspec", :git => "git://github.com/rspec/rspec.git"
gem "rspec-core", :git => "git://github.com/rspec/rspec-core.git"
gem "rspec-expectations", :git => "git://github.com/rspec/rspec-expectations.git"
gem "rspec-mocks", :git => "git://github.com/rspec/rspec-mocks.git"
gem 'webrat'
end
I am using Ruby 1.9.3, Rails 3.2.1, RVM 1.10.2, Bundler 1.0.21
I'm new to Rails. I am making my way through literature and have just stumbled across the 'rails console' command.
When I type that in, I get:
Loading development environment (Rails 3.0.7)
In turn, the development environment never loads. I've waited quite a few minutes, too.
I did see some of the other related posts to this topic/problem, but in all instances, it seemed that individuals were trying to speed up their environment load time -- not simply get their environment to load in the first place.
As for my Gemfile:
source 'http://rubygems.org'
gem 'rails', '3.0.7'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3'
# Use unicorn as the web server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
gem 'ruby-debug'
# gem 'ruby-debug19', :require => 'ruby-debug'
# Bundle the extra gems:
# gem 'bj'
# gem 'nokogiri'
# gem 'sqlite3-ruby', '1.3.1', :require => 'sqlite3'
# gem 'aws-s3', :require => 'aws/s3'
# Bundle gems for the local environment. Make sure to
# put test-only gems in this group so their generators
# and rake tasks are available in development mode:
# group :development, :test do
# gem 'webrat'
# end
Just update your bundler, it may be of any of the older gems is conflicting with latest one, this type of issues arises.
'gem install bundler'
and then 'bundle install'
If you find again error, then delete the Gemfile.lock file, then run bundle install. It may resolve the issue.
I would install RVM and not use the built in Ruby etc.
Install RVM
I've upgraded my app from using config.gem to a Gemfile with bundler and have noticed that my unit tests have now stopped running. It's a bit strange and I'm not entirely sure where to start looking.
When I run rake test:units --trace I can see my environment being setup and it lists the files it intends to execute, but then it just returns.
It does the same thing if I try to run one individual file using something like: rake -I"lib:test" test/unit/foo.rb or using autotest.
It's all very strange. It's as if the files are being loaded but the actual unit tests are not being run.
I'm using shoulda and fast_context and I thought these might be the problem but if I include a unit test using the standard def test_ syntax it still doesn't get run so I don't think it's those.
Any hints or pointers would be greatly appreciated. I feel like I'm coding blind until I can get them working again!
So here's where I am now:
My reasons for using bundler are for installing dependencies on heroku and because I wanted to use a gem sourced from a git repo on github. The long and the short of it is that I've removed the preinitializer for bundler and have gone back to using config.gem. To get around the fact that I can't use a github repo using config.gem I've pushed out my own copy to rubygems. Was this the right move?
Here's the preinitializer.rb
begin
require "rubygems"
require "bundler"
rescue LoadError
raise "Could not load the bundler gem. Install it with `gem install bundler`."
end
if Gem::Version.new(Bundler::VERSION) <= Gem::Version.new("0.9.24")
raise RuntimeError, "Your bundler version is too old for Rails 2.3." +
"Run `gem install bundler` to upgrade."
end
begin
# Set up load paths for all bundled gems
ENV["BUNDLE_GEMFILE"] = File.expand_path("../../Gemfile", __FILE__)
Bundler.setup
rescue Bundler::GemNotFound
raise RuntimeError, "Bundler couldn't find some gems." +
"Did you run `bundle install`?"
end
I don't know how the .gems file would be useful because it's a heroku only thing and I'd have to hunt through git for it, but here's my gemfile.
source :gemcutter
gem 'rails', '2.3.9'
gem 'pg'
gem 'minitest'
gem 'RedCloth'
gem 'erubis'
#gem 'memcached'
gem 'daemons'
gem 'resque'
gem 'inherited_resources', '1.0.6'
gem 'clearance', '0.8.8'
gem 'acl9'
gem 'sprockets'
gem 'aws-s3'
gem 'paperclip', '2.3.1.1'
gem 'rmagick', '2.12.2'
gem 'jonnii-cheddargetter', '0.1.3'
gem 'attribute_normalizer'
gem 'formtastic', '1.1.0.beta'
gem 'will_paginate', '2.3.14'
gem 'hoptoad_notifier'
gem 'mixpanel_client'
gem 'sunspot'
gem 'websolr-sunspot_rails'
gem 'geokit'
gem 'ri_cal'
gem 'jonnii-yelp'
group :development, :test do
gem 'test-spec'
gem 'shoulda'
gem 'redgreen'
gem 'factory_girl'
gem 'populator'
gem 'faker'
gem 'ZenTest'
gem 'autotest-rails'
gem 'webrat'
gem 'cucumber'
gem 'cucumber-rails'
gem 'database_cleaner'
gem 'parallel'
gem 'hydra'
gem 'heroku'
gem 'taps'
gem 'ruby-prof'
gem 'treetop'
gem 'rspec'
gem 'rspec-rails'
end
Got the same problem.Just remove the gem 'hydra' will get the unit test back to normal
Do you have this at the end of your config/boot.rb file:
class Rails::Boot
def run
load_initializer
Rails::Initializer.class_eval do
def load_gems
#bundler_loaded ||= Bundler.require :default, Rails.env
end
end
Rails::Initializer.run(:set_load_path)
end
end
(from http://gembundler.com/rails23.html)
I recently had trouble running specs for a project. The reason was that I was missing a line from config/application.rb. Nowadays that line pops in by default when you create a new rails 3 project, but if your project has been initialized some time ago it might be missing.
# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)
Heroku made all its apps upgrade to the latest version of bundler (0.9.4).
I followed all the instructions found on the README (including the upgrading instructions). But once I upgrade my application no longer runs. For example i get
NoMethodError (undefined method `acts_as_taggable_on' for #<Class:0x1b7f614>):
My Gemfile is as follows
source 'http://gemcutter.org'
source 'http://gems.github.com'
gem "rails", "2.3.5", :require => nil
gem 'will_paginate', '2.3.11'
gem 'jackdempsey-acts_as_commentable', :require => 'acts_as_commentable'
gem 'acts-as-taggable-on'
# Authorization
gem 'authlogic'
gem 'authlogic-oid', :require => 'authlogic_openid'
gem 'ruby-openid', :require => 'openid'
#Authentication
gem 'cancan'
gem 'gravtastic', '>= 2.1.0'
# Exception Notification
gem 'hoptoad_notifier'
# Search (Note ties us to Postgres)
gem 'texticle'
gem 'pg'
My boot.rb,preinitializer.rb are as instructed in this gist
Thanks for your help.
Please don't ask me how this works, but I had the same exact issue with what seemed to be failing actionpack dependencies or paths or something.
I used all of the gist referred to by the bundler team: http://gist.github.com/302406
But I tweaked my config/boot.rb script to this:
class Rails::Boot
def run
load_initializer
extend_environment
Rails::Initializer.run(:set_load_path)
end
def extend_environment
Rails::Initializer.class_eval do
old_load = instance_method(:load_gems)
define_method(:load_gems) do
old_load.bind(self).call
Bundler.require :default, RAILS_ENV
end
end
end
end
I dont know why my config variables were different, but for some reason they are. I'm sure someone who understands the internals a bit better than I do can explain it.
*For heroku you'll also have to have the postgres "pg" gem installed. This was another minor annoyance. Depending on how you install postgres, finding pg_config can be another headache. Let me know if you need help with this.