20

UPDATES - 2015-12-09

I'm using Rails 4.2.4 with ruby 2.2.3.

Here is my spec_helper.rb file (generated by generator):

RSpec.configure do |config|
  require 'rails/all'
  require 'json_spec'
  require 'devise'
  require 'rspec/rails'

  config.infer_spec_type_from_file_location!

  config.include JsonSpec::Helpers
  config.include Devise::TestHelpers, type: :request

  config.include Devise::TestHelpers, type: :controller # https://github.com/plataformatec/devise#test-helpers

  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end
end

Here is my rails_helper.rb:

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)

abort('The Rails environment is running in production mode!') if Rails.env.production?

# ADDED BY AS #
require 'support/database_cleaner'
require 'yarjuf'
# END OF ADDED BY AS #

require 'spec_helper'
require 'rspec/rails'

# ADDED BY AS #
if ENV['COVERAGE']
  require 'simplecov'
  require 'simplecov-csv'
  SimpleCov.formatter = SimpleCov::Formatter::CSVFormatter
  SimpleCov.coverage_dir(ENV['COVERAGE_REPORTS'])
  SimpleCov.start 'rails'
end
# END OF ADDED BY AS #

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  config.use_transactional_fixtures = true

  config.infer_spec_type_from_file_location!

  config.filter_rails_from_backtrace!
end

On Gemfile:

source 'https://rubygems.org'
ruby '2.2.3'
# ruby '2.1.0', :engine => 'rbx', :engine_version => '2.2.6' #rubinius
# ruby '1.9.3', engine: 'jruby', engine_version: '1.7.10'

source 'https://be79af0d:c5a6f6bd@gems.contribsys.com/' do
  gem 'sidekiq-pro', '>= 3'
end

gem 'mime-types',               '>= 2.6', require: 'mime/types/columnar'
gem 'activerecord-import'
gem 'aasm',                     '~> 4.5.0'
gem 'activerecord-colored_log_subscriber'
gem 'aws-sdk-v1'                # Needed by paperclip @ v4.2.1, consider the removal after paperclip gem updates will support awk-sdk v2.
gem 'aws-sdk',                  '~> 2'
gem 'active_model_serializers'
gem 'ancestry',                 '~> 2.1.0' # Gestione delle gerarchie ad albero
gem 'auxilium',                 path: 'engines/auxilium'
gem 'barby',                    '~> 0.6.1' # Codice a barre
gem 'bootstrap-kaminari-views', '~> 0.0.5'
gem 'cancancan',                '~> 1.13'
gem 'crummy',                   '~> 1.8.0'
gem 'dalli'
gem 'devise',                  '~> 3.5.0'
gem 'devise_security_extension'
gem 'devise-async',            '~> 0.10.1'
gem 'email_validator',          '>= 1.5.0'
gem 'flipclockjs-rails',        '~> 0.7.4', git: 'https://github.com/knightq/flipclockjs-rails.git'
gem 'font_assets'
gem 'google_visualr',           '~> 2.5.1' # Grafici
gem 'hamlit-rails'
gem 'hirefire-resource',        '~> 0.3.8'
gem 'htmlentities',             '~> 4.3.4', require: false
gem 'ice_cube',                 '~> 0.12.1' # Ricorrenze
gem 'jhtmlarea',                '~> 0.1.0' # formattazione testo
gem 'kaminari',                 '~> 0.16.2'
gem 'mailboxer',                '~> 0.13.0' # Messaggistica interna
gem 'memcachier'
gem 'mini_magick',              '~> 4.3.4', require: false
gem 'newrelic_rpm'
gem 'paperclip',                '~> 4.3'
gem 'paginate_alphabetically',  '~> 0.3.3', git: 'https://github.com/edendevelopment/paginate_alphabetically.git', branch: 'bundler'
gem 'paper_trail',              '~> 4.0.0' # Record versioning
gem 'pdfkit',                   '~> 0.8.2'
gem 'pg',                       '~> 0.18.4' # PostgreSql
gem 'heroku-api', require: false
gem 'prawn',                    '~> 1.0.0'
gem 'prawn-print'
gem 'prawn-svg',                '~> 0.22'
gem 'prawn-templates',          '~> 0.0.3'
gem 'rack-attack'
gem 'rails',                    '~> 4.2.4'
gem 'rails-jquery-autocomplete'
gem 'rainbow',                  '~> 2.0.0', require: false
gem 'ransack',                  '~> 1.7.0' # Ricerche
gem 'render_anywhere',          '~> 0.0.12' # Gemma per il rendering di view in workers
gem 'responders',               '~> 2.0'
gem 'rollbar',                  '~> 2.6'  # Gestione eccezioni
gem 'rqrcode',                  '~> 0.7.0' # Codice QR
gem 'savon',                    '~> 2.0'
gem 'scout_apm'
gem 'sinatra',                  '>= 1.3.0', require: nil
# gem 'sidekiq',                  '>= 3.3.2'
gem 'sidekiq-limit_fetch',      '>= 3.0.1'
gem 'sidekiq-status',           '~> 0.5.1'
gem 'sidekiq-statistic',        '~> 1.2'
gem 'sorted',                   '~> 2.0.3'
gem 'sorted-actionview',        '~> 0.0.1'
gem 'sorted-activerecord',      '~> 0.1.2'
gem 'switch_user',              '~> 1.2.0'
gem 'jc-validates_timeliness',  '~> 3.0'
gem 'when_easter',              '~> 0.3.1' # Calcolo pasqua
gem 'wicked',                   '~> 1.2.1' # Wizards
gem 'wkhtmltopdf-binary',       '~> 0.9.9.1'
gem 'rubyzip', require: false
gem 'ruby-progressbar', require: false
gem 'templatar'
gem 'tunemygc'
gem 'rqrcode-rails3'
gem 'strip_attributes'
gem 'analytics-ruby', '~> 2.0.0', require: 'segment/analytics'
gem 'activerecord-session_store', '~> 0.1.2'
gem 'jquery-ui-themes',         '~> 1.11.4', git: 'https://github.com/knightq/jquery-ui-themes-rails.git', branch: 'jquery-ui-1.11.x'
# Rubinius-test dependencies
# gem 'racc', platform: :rbx
# gem 'rubysl', platform: :rbx

# jruby-test dependencies
# gem 'activerecord-jdbcpostgresql-adapter'
# gem 'rmagick4j'

# TODO: Test rubinius and jruby with puma

group :production, :staging do
  # gem 'unicorn',                  '~> 4.7.0'
  gem 'kgio'
  gem 'puma',                    '~> 2.15.3'
  gem 'rack-cache'
  gem 'rails_12factor' # see http://stackoverflow.com/questions/9027403/rails-2-3-style-plugins-and-deprecation-warnings-running-task-in-heroku
end

gem 'bootstrap-sass',            '~> 2.3.2.2'
gem 'chosen-rails',              '~> 1.4.3'
gem 'coffee-rails',              '~> 4.1.0'
gem 'momentjs-rails'
gem 'fullcalendar-rails',        '~> 2.5.0'
gem 'jquery_context_menu-rails', '~> 0.0.4'
gem 'jquery-fileupload-rails',   '~> 0.4.1'
gem 'jquery-rails',              '~> 4.0.5'
gem 'jquery-ui-rails'
gem 'jqgrid-jquery-rails'
gem 'sass-rails',                '~> 5.0.4'
gem 'sprockets'
gem 'select2-rails',             '~> 3.5.9.3'
gem 'uglifier',                  '~> 2.7.2'
gem 'jquery-infinite-pages',     '~> 0.2.0'

group :development   do
  # gem 'better_errors'
  # gem 'binding_of_caller'
  gem 'derailed'
  gem 'bullet'
  gem 'web-console',               '~> 2.0'
  gem 'hirb',                      '~> 0.7.1'
  gem 'hub', require: false
  gem 'i18n_yaml_sorter'
  gem 'meta_request'
  gem 'quiet_assets'
  gem 'peek'
  gem 'peek-rblineprof'
  gem 'peek-git'
  gem 'peek-gc'
  gem 'peek-performance_bar'
  gem 'peek-pg'
  gem 'pygments.rb', require: false
  # gem 'rack-mini-profiler'
  gem 'rubocop'
  gem 'spring'
  gem 'stackprof'
  gem 'thin'
  gem 'traceroute' # find the dead routes and actions, see: https://github.com/amatsuda/traceroute
end

group :staging do
  gem 'httplog'
end

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

group :development, :test do
  gem 'capybara', '~> 2.5.0'
  gem 'database_cleaner'
  gem 'factory_girl_rails', '~> 4.0'
  gem 'jasmine-rails'
  gem 'pry-rails'
  gem 'pry-nav'
  gem 'rspec-rails', '~> 3.4.0'
  gem 'seed-fu', '~> 2.3'
end

group :test do
  gem 'yarjuf'
  gem 'json_spec', require: false
  gem 'simplecov', require: false
  gem 'simplecov-csv', require: false
end

By now, I've only one spec class on /spec/models/my_model_spec.rb:

require 'rails_helper'

describe MyModel do

end

The server starts and run correctly, but when I try to launch bundle exec rspec spec o even simply rspec spec I get:

/Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:146:in `to_app': missing run or map statement (RuntimeError)
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:160:in `block in generate_map'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:160:in `each'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:160:in `generate_map'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/builder.rb:145:in `to_app'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/capybara-2.5.0/lib/capybara/rails.rb:13:in `<top (required)>'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `block in require'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-rails-3.4.0/lib/rspec/rails/vendor/capybara.rb:7:in `<top (required)>'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `block in require'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-rails-3.4.0/lib/rspec/rails.rb:13:in `<top (required)>'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `block in require'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require'
    from /Users/andreasalicetti/Development/epersonam/spec/spec_helper.rb:23:in `block in <top (required)>'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.1/lib/rspec/core.rb:97:in `configure'
    from /Users/andreasalicetti/Development/epersonam/spec/spec_helper.rb:19:in `<top (required)>'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1295:in `require'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1295:in `block in requires='
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1295:in `each'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1295:in `requires='
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.1/lib/rspec/core/configuration_options.rb:109:in `block in process_options_into'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.1/lib/rspec/core/configuration_options.rb:108:in `each'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.1/lib/rspec/core/configuration_options.rb:108:in `process_options_into'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.1/lib/rspec/core/configuration_options.rb:21:in `configure'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:101:in `setup'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:88:in `run'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:73:in `run'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:41:in `invoke'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-core-3.4.1/exe/rspec:4:in `<top (required)>'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/bin/rspec:23:in `load'
    from /Users/andreasalicetti/.rbenv/versions/2.2.3/bin/rspec:23:in `<main>'
Andrea Salicetti
  • 2,423
  • 24
  • 37
  • Try adding `require 'rubygems'` to the top of your spec_helper. –  May 22 '14 at 13:13
  • `rspec spec/models/my_model_spec.rb` does not work? – manu29.d May 22 '14 at 14:00
  • nope, it does not work. It crashes on boot, exactly on the `require File.expand_path('../../config/environment', __FILE__)` – Andrea Salicetti May 22 '14 at 14:04
  • Could you try commenting out papertrail gem in your Gemfile and run `bundle install`, then try rspec again. Just to confirm if your problem is specifically with papertrail gem or with rspec. If this works then try upgrading papertrail to latest version (3.0.2) –  May 26 '14 at 12:48
  • Already done: it is not something related to that gem. – Andrea Salicetti May 26 '14 at 13:43
  • 3
    The error clearly _is_ related to the gem. So how does the error change if you exclude the gem? – nathanvda May 29 '14 at 15:23
  • I've updated the description with console output post `paper_trail` gem remotion and even after further removal of `ransack` gem. It does not solve: shall I go on removing gems? – Andrea Salicetti May 29 '14 at 15:47
  • It looks like when the external libraries are loaded, rails has not yet been loaded... – Andrea Salicetti May 29 '14 at 15:49
  • I think the problem is in one of the models. – MZaragoza Nov 14 '14 at 21:08
  • Is ARel in your Gemfile.lock? – trueinViso Nov 18 '14 at 01:50
  • Also, maybe try adding this to the spec_helper file: require 'paper_trail/frameworks/rspec' – trueinViso Nov 18 '14 at 02:12
  • can you specify the following version and try updating your gems with bundle update rspec-rails and see if the problem still exists? gem 'rspec-rails', '~> 3.0' – Rubyrider Nov 18 '14 at 04:58
  • The first file contents in your question might need to be stored in `spec\rails_helper.rb`, but you said `spec\spec_helper.rb`. – sealocal Nov 19 '14 at 07:15
  • what do you get when you try and run `bundle exec rspec .` ? – TomDunning Jan 15 '15 at 10:42
  • @Isotope I've added the output you asked at the bottom of my question. – Andrea Salicetti Jan 19 '15 at 09:53
  • @AndreaSalicetti, I see in your Gemfile, that you have fixed most gems to a version, however the rspec gem (and some others) is not. Could it be that you have updated this in the past, now leading to all these errors? Maybe looking the version up per "gem list" and trying to revert to an older version could help. – benjamin Dec 08 '15 at 06:45
  • I've updated my question, that was quite obsolete due to later changes – Andrea Salicetti Dec 09 '15 at 17:03
  • @AndreaSalicetti, as it seems rspec related you could try running tests against the rspec gem itself and see if we obtain a more verbose error message; possibly this could serve as a starting point https://stackoverflow.com/questions/8586591/an-easy-way-to-run-tests-on-a-gem – benjamin Dec 09 '15 at 22:12

5 Answers5

1

Please try removing one or both of these lines:

require File.expand_path("../../config/environment", __FILE__)
require 'rspec/autorun'

My RSpec runs well without them.

Dan Kohn
  • 33,811
  • 9
  • 84
  • 100
1

The issue isn't the gems themselves, it's the order things are loaded. Please change the top lines of the spec helper to this:

ENV["RAILS_ENV"] ||= 'test'
require 'rubygems'
require 'rspec/rails'
require 'rspec/autorun'
require File.expand_path("../../config/environment", __FILE__)

Basically, I think you're trying to load the application before rubygems and other dependencies which may be why @dankohn 's answer failed.

TomDunning
  • 4,829
  • 1
  • 26
  • 33
  • nope, I've tried also in this way, but it raise an error on line 4 (`require 'rspec/rails'`) saying: /.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/actionpack-4.0.12/lib/action_view/template.rb:8:in `': uninitialized constant ActiveSupport::Autoload (NameError) – Andrea Salicetti Nov 20 '14 at 15:53
  • 1
    "actionpack-4.0.12" but you say you're using rails 3.2? There's a problem there, it should be 3.x. You've run `bundle` right? I'm thinking there might be an issue with your rbenv / bundler config – TomDunning Nov 21 '14 at 14:16
1

Use this gem shoulda-matchers

It is one of the best gem to write rspec for associations

Mukesh
  • 921
  • 10
  • 23
1

In your Gemfile, most gems are fixed to a version, as is the rails gem. However rspec and pry are not fixed, hence get updated. Probably finding out what the initial version of your rspec was, fixing it in the gemfile and running a bundle install helps.

In case you have not explicitly uninstalled gems, gem list shows you earlier versions.

benjamin
  • 2,185
  • 1
  • 14
  • 19
0

In your code replace Version with PaperTrail::Version wherever you are using Version.

leena
  • 302
  • 3
  • 7