Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot run rspec (undefined method `belongs_to')

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:[email protected]/' 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>'
like image 935
Andrea Salicetti Avatar asked May 22 '14 12:05

Andrea Salicetti


4 Answers

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.

like image 67
Dan Kohn Avatar answered Nov 11 '22 17:11

Dan Kohn


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.

like image 1
TomDunning Avatar answered Nov 11 '22 18:11

TomDunning


Use this gem shoulda-matchers

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

like image 1
Mukesh Avatar answered Nov 11 '22 17:11

Mukesh


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.

like image 1
benjamin Avatar answered Nov 11 '22 16:11

benjamin