我使用Rails 4.2.4和ruby 2.2.3。
这是我的spec_helper.rb文件(由生成器生成):
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
这是我的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
在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
到目前为止,我/spec/models/my_model_spec.rb
上只有一个规范类:
require 'rails_helper'
describe MyModel do
end
服务器启动并正常运行,但当我尝试启动bundle exec rspec spec
时,即使只是rspec spec
,我得到:
/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>'
答案 0 :(得分:1)
请尝试删除以下一行或两行:
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/autorun'
我的RSpec在没有它们的情况下运行良好。
答案 1 :(得分:1)
问题本身并不是宝石,而是装载物品的顺序。请将规范助手的顶行更改为:
ENV["RAILS_ENV"] ||= 'test'
require 'rubygems'
require 'rspec/rails'
require 'rspec/autorun'
require File.expand_path("../../config/environment", __FILE__)
基本上,我认为您正在尝试在rubygems和其他依赖项之前加载应用程序,这可能是@dankohn的答案失败的原因。
答案 2 :(得分:1)
使用此gem shoulda-matchers
它是为关联编写rspec的最佳宝石之一
答案 3 :(得分:1)
在你的Gemfile中,大多数宝石被固定为一个版本,rails gem也是如此。但是rspec和pry不是固定的,因此得到更新。可能会找出你的rspec的初始版本是什么,修复它在gemfile中并运行 bundle install 有帮助。
如果您没有明确卸载gems, gem list 会显示早期版本。