`require': cannot load such file -- capybara/rspec (LoadError) - ruby-on-rails

I tried to test my project. It was working before and I don't know what i did to whenever i type bundle exec rspec spec/ it says cannot load such file -- capybara/rspec (LoadError). And Please I need an advice to which one is good for testing my MVC in ruby on rails as a newbie.
Gemfile:
------------
group :test do
# Pretty printed test output
gem 'turn', :require => false
gem 'minitest'
gem 'capybara', '1.1.2'
gem 'rb-inotify', '0.8.8'
gem 'libnotify', '0.5.9'
gem 'guard-spork', '0.3.2'
gem 'spork', '0.9.0'
gem 'spork-testunit'
gem 'guard-test'
gem 'ruby-prof'
gem 'factory_girl_rails', '1.4.0'
if RUBY_PLATFORM =~ /linux/
gem 'capybara-webkit'
end
gem 'launchy'
end
group :development, :test do
gem 'rspec-rails', '2.10.0'
gem 'guard-rspec', '0.5.5'
end
spec_helper:
require 'rubygems'
#require 'factory_girl'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require 'capybara/rails'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :rspec
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = false
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
#Capybara.default_driver = :selenium
Capybara.javascript_driver = :webkit
end
Error:
/home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:251:in `require': cannot load such file -- capybara/rspec (LoadError)
from /home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:251:in `block in require'
from /home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:236:in `load_dependency'
from /home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:251:in `require'
from /home/IN4SYSTEMS/sri.kalai/Documents/promaster/spec/spec_helper.rb:11:in `<top (required)>'
from /home/IN4SYSTEMS/sri.kalai/Documents/promaster/spec/helpers/loc/epcs_helper_spec.rb:1:in `require'
from /home/IN4SYSTEMS/sri.kalai/Documents/promaster/spec/helpers/loc/epcs_helper_spec.rb:1:in `<top (required)>'
from /home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `load'
from /home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `block in load_spec_files'
from /home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `map'
from /home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `load_spec_files'
from /home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/command_line.rb:22:in `run'
from /home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:66:in `rescue in run'
from /home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:62:in `run'
from /home/IN4SYSTEMS/sri.kalai/Desktop/gems/ruby/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/runner.rb:10:in `block in autorun'
Thanks in advance!!!

I hope you did install all required gems? Using bundle install BTW did you change Gemfile, in that case you would be required to install Gems using bundle install.
You could check list of gems available by gem list
Regarding second part of the question, which testing API to use. It is matter of choice. You could stick with rpsec and if you find it falls short of your expectation, then look out for change.

Try gem query --local to see if capybara-screenshot appears in your list of installed gems. (For some reason, this rendered a different list than gem list for me.)
If you don't see it, then try gem install capybara-screenshot. It is not included in the installation of capybara, so seeing capybara in your list of gems alone is not sufficient!

I had the same problem today and I fixed it in the following way:
Comment in file spec_helper.rb
next raws
require 'capybara/rspec'
require 'capybara/rails',
Remove
/usr/local/rvm/gems/ruby-1.9.3-p327#olha/gems/capybara-webkit-1.1.0
from my HDD space.

Related

bundle exec rspec spec/requests/static_pages_spec.rb and error

I am using Michael Hartl's ruby on rail tutorial.
When I try to execute the command:bundle exec rspec spec/requests/static_pages_spec.rb
I get the following errors:
mudit#Bat:~/rails_projects/sample_app$ bundle exec rspec spec/requests/static_pages_spec.rb
/var/lib/gems/2.3.0/gems/activesupport-3.2.3/lib/active_support/values/time_zone.rb:268: warning: circular argument reference - now
SECURITY WARNING: No secret option provided to Rack::Session::Cookie.
This poses a security threat. It is strongly recommended that you
provide a secret to prevent exploits that may be possible from crafted
cookies. This will not be supported in future versions of Rack, and
future versions will even invalidate your existing user cookies.
Called from: /var/lib/gems/2.3.0/gems/actionpack-3.2.3/lib/action_dispatch/middleware/session/abstract_store.rb:28:in `initialize'.
Rack::File headers parameter replaces cache_control after Rack 1.5.
/var/lib/gems/2.3.0/gems/rspec-rails-2.13.1/lib/rspec/rails/fixture_support.rb:19:in `block (2 levels) in <module:FixtureSupport>': private method `fixture_path' called for #<RSpec::Core::Configuration:0x000000017db6e8> (NoMethodError)
Did you mean? fixture_path=
from /var/lib/gems/2.3.0/gems/rspec-rails-2.13.1/lib/rspec/rails/fixture_support.rb:17:in `class_eval'
from /var/lib/gems/2.3.0/gems/rspec-rails-2.13.1/lib/rspec/rails/fixture_support.rb:17:in `block in <module:FixtureSupport>'
from /var/lib/gems/2.3.0/gems/activesupport-3.2.3/lib/active_support/concern.rb:119:in `class_eval'
from /var/lib/gems/2.3.0/gems/activesupport-3.2.3/lib/active_support/concern.rb:119:in `append_features'
from /var/lib/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:791:in `include'
from /var/lib/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:791:in `safe_include'
from /var/lib/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:785:in `block in configure_group'
from /var/lib/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:783:in `each'
from /var/lib/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:783:in `configure_group'
from /var/lib/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/world.rb:47:in `configure_group'
from /var/lib/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:290:in `set_it_up'
from /var/lib/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:241:in `subclass'
from /var/lib/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:228:in `describe'
from /var/lib/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/dsl.rb:18:in `describe'
from /home/mudit/rails_projects/sample_app/spec/requests/static_pages_spec.rb:3:in `<top (required)>'
from /var/lib/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load'
from /var/lib/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
from /var/lib/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each'
from /var/lib/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
from /var/lib/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run'
from /var/lib/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
from /var/lib/gems/2.3.0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'
My static_pages_spec.rb is as follows:
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
end
end
My spec_helper.rb is as follows:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.include Capybara::DSL
end
My Gemfile is as follows:
source 'https://rubygems.org'
gem 'rails', '3.2.3'
group :development, :test do
gem 'sqlite3'
gem 'rspec-rails', '2.13.1'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
end
gem 'sass-rails', '3.2.4'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
group :production do
gem 'pg', '0.12.2'
end
Try comment this line
config.fixture_path = "#{::Rails.root}/spec/fixtures"
You may see here
https://github.com/intridea/rails_wizard.web/issues/39
http://procbits.com/2011/08/18/using-mongoid-with-rspec

Railties cannot load such file

First, you should know upfront I am a newb to rails so I apologize if this is a dumb mistake but I've been racking my brain on it and thought I'd ask for some help.
I am currently unable to start my local rails server all of the sudden. Not sure what I've done to cause this as I was able to start it earlier today just fine. Only thing I've done in between it working and not was trying to figure out how to push the app to Heroku.
When running rails s while in root of the app I get this error:
/Users/jonathanmacdonald/code/lrbp/config/application.rb:6:in `require': cannot load such file -- active_job/railtie (LoadError)
from /Users/jonathanmacdonald/code/lrbp/config/application.rb:6:in `<top (required)>'
from /Library/Ruby/Gems/2.0.0/gems/railties- 4.2.3/lib/rails/commands/commands_tasks.rb:78:in `require'
from /Library/Ruby/Gems/2.0.0/gems/railties- 4.2.3/lib/rails/commands/commands_tasks.rb:78:in `block in server'
from /Library/Ruby/Gems/2.0.0/gems/railties- 4.2.3/lib/rails/commands/commands_tasks.rb:75:in `tap'
from /Library/Ruby/Gems/2.0.0/gems/railties- 4.2.3/lib/rails/commands/commands_tasks.rb:75:in `server'
from /Library/Ruby/Gems/2.0.0/gems/railties- 4.2.3/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Library/Ruby/Gems/2.0.0/gems/railties- 4.2.3/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/jonathanmacdonald/code/lrbp/bin/rails:10:in `require'
from /Users/jonathanmacdonald/code/lrbp/bin/rails:10:in `<top (required)>'
from /Library/Ruby/Gems/2.0.0/gems/spring- 1.6.2/lib/spring/client/rails.rb:28:in `load'
from /Library/Ruby/Gems/2.0.0/gems/spring- 1.6.2/lib/spring/client/rails.rb:28:in `call'
from /Library/Ruby/Gems/2.0.0/gems/spring- 1.6.2/lib/spring/client/command.rb:7:in `call'
from /Library/Ruby/Gems/2.0.0/gems/spring- 1.6.2/lib/spring/client.rb:28:in `run'
from /Library/Ruby/Gems/2.0.0/gems/spring-1.6.2/bin/spring:49:in `<top (required)>'
from /Library/Ruby/Gems/2.0.0/gems/spring- 1.6.2/lib/spring/binstub.rb:11:in `load'
from /Library/Ruby/Gems/2.0.0/gems/spring- 1.6.2/lib/spring/binstub.rb:11:in `<top (required)>'
from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/jonathanmacdonald/code/lrbp/bin/spring:13:in `<top (required)>'
from bin/rails:4:in `load'
from bin/rails:4:in `<main>'
Gemfile
source 'https://rubygems.org'
group :production do
gem 'rails', '4.2.3'
gem 'pg'
gem 'rails_12factor'
gem 'bootstrap-sass'
end
group :development, :test do
gem 'sqlite3'
gem 'byebug'
gem 'web-console', '~> 2.0'
gem 'spring'
gem 'binding_of_caller'
gem 'better_errors'
end
config/application.rb
require File.expand_path('../boot', __FILE__)
require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Lrbp
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*. {rb,yml}').to_s]
# config.i18n.default_locale = :de
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
config.assets.initialize_on_precompile = false
end
end
Things I've tried after research
bundle exec rails server ->> Still won't start and produces the exact same error
Temporarily mv Gemfile.lock .. so that I could regenerate it with bundle install ->> No change. Same error.
gem pristine --all ->> No change. Same error.
gem uninstall rails and then afterwards gem install rails ->> No change. Same error.
Setup
OS X 10.11.2
Rails 4.2.3
Ruby 2.0.0
What can I look for or try? Any help is appreciated. Let me know if you need any more information in order to help.
Your application.rb will look like
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(:default, Rails.env)
module Lrbp
class Application < Rails::Application
you do not require to each rails module separately like
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "sprockets/railtie"
you can use.
require 'rails/all'
it will do something like this
%w(
active_record
action_controller
action_view
action_mailer
rails/test_unit
sprockets
).each do |framework|
begin
require "#{framework}/railtie"
rescue LoadError
end
end
and Gemfile
source 'https://rubygems.org'
gem 'rails', '4.2.3'
gem 'pg'
gem 'rails_12factor'
gem 'bootstrap-sass'
group :development, :test do
gem 'sqlite3'
gem 'byebug'
gem 'web-console', '~> 2.0'
gem 'spring'
gem 'binding_of_caller'
gem 'better_errors'
end
Then you should do
bundle install & bundle exec rails s

Issue with Paperclip and Rspec: undefined method `has_attached_file'

I'm adding Rspec tests to a project that didn't have rspec before, or automated tests at all. It is running on a Windows 7 box. When I run rspec, it shows the error:
undefined method `has_attached_file'
The main issue is that I'm writing just a simple test for a model that is not using Paperclip at all. Paperclip is used on another model.
I've researched extensively, trying to include the gem in test environment but nothing seems to solve the issue.
Is there a way of skipping or forcing the inclussion of paperclip when I run the rspec tests?
My gemfile has, among other things, the following:
source 'http://rubygems.org'
ruby "1.9.2"
gem 'rails', '3.0.20'
...
gem "paperclip", :git => 'git://github.com/thoughtbot/paperclip.git'
gem 'aws-s3'
gem 'aws-sdk'
group :test, :development do
gem 'rspec-rails', '~> 2.14.0'
end
group :test do
gem 'sqlite3'
gem 'capybara', '2.0'
gem 'selenium-webdriver'
gem 'shoulda-matchers'
gem 'database_cleaner'
gem 'launchy', '2.2.0'
gem 'factory_girl_rails'
end
I run the rspec command:
bundle exec rake spec
I get the following error:
C:/Users/MAC/.pik/rubies/Ruby-192-p290/bin/ruby.exe -S rspec ./spec/models/filter_category_spec.rb ./spec/models/filter_spec.rb
C:/Users/MAC/.pik/rubies/Ruby-192-p290/lib/ruby/gems/1.9.1/gems/attr_encrypted-1.2.1/lib/attr_encrypted.rb:241:in `method_missing': undefined method `has_a
ttached_file' for #<Class:0x5278770> (NoMethodError)
from C:/Users/MAC/.pik/rubies/Ruby-192-p290/lib/ruby/gems/1.9.1/gems/activerecord-3.0.20/lib/active_record/base.rb:1018:in `method_missing'
from C:/Users/MAC/.pik/rubies/Ruby-192-p290/lib/ruby/gems/1.9.1/gems/attr_encrypted-1.2.1/lib/attr_encrypted/adapters/active_record.rb:50:in `metho
d_missing_with_attr_encrypted'
....
from C:/Users/MAC/.pik/rubies/Ruby-192-p290/lib/ruby/gems/1.9.1/gems/railties-3.0.20/lib/rails/application.rb:77:in `method_missing'
from C:/Avity/DS Candidates Tracking System/ds-cts/config/environment.rb:5:in `<top (required)>'
from C:/Avity/DS Candidates Tracking System/ds-cts/spec/spec_helper.rb:3:in `require'
from C:/Avity/DS Candidates Tracking System/ds-cts/spec/spec_helper.rb:3:in `<top (required)>'
from C:/Avity/DS Candidates Tracking System/ds-cts/spec/models/filter_category_spec.rb:1:in `require'
My spec_helper file is:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = false
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
#Database cleaner
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
#Use chrome driver
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
Capybara.current_driver = :selenium_chrome
end
Any ideas of what should I do to make this work properly?
EDIT It seems the issue comes before loading spec_helper.rb
If I make changes on this file, like adding errors on purpose, nothing happens and the issue above keeps happening.
The only way I have to run the tests is by commenting the has_attached_file line on my model and adding the group option to the paperclip gem on my Gemfile, to avoid having it on test environment:
gem "paperclip", :git => 'git://github.com/thoughtbot/paperclip.git', :group=>[:development,:production]
Not sure how to solve this without skipping Paperclip from test env.
The problem seems to me to be with shoulda-matchers and paperclip
Add to your spec_helper.rb file
require "paperclip/matchers"
below where you are
require "capybara/rspec"
And add
config.include Paperclip::Shoulda::Matchers
below
RSpec.configure do |config|
Check out the shoulda-matchers documentation for paperclip
Hope this helps
I ran into this issue when trying to use Paperclip in a gem that has ActiveRecord, but not Rails. I was able to fix this for tests by adding a spec/support/paperclip.rb file with the following:
require 'paperclip/railtie'
RSpec.configure do |config|
# Bind paperclip to enable ActiveRecord #has_attached_file method
Paperclip::Railtie.insert
end
As also suggested, this is a good place to add config.include Paperclip::Shoulda::Matchers.
But I'm using Rails 4.2 and Rspec 3, so I think the order of things loaded is much different than yours.

Error in testing ruby on rails using rspec in tutorial

I am newbie for ruby on rails i am just reading the http://ruby.railstutorial.org/chapters/static-pages#top as the tutorial saying, i have created the sample_app bundle or project. Now in section 3.2.1 i am testing the app.
first i run the following from the cmd
$ rails generate integration_test static_pages
This created the static_pages_spec.rb in the spec/requests directory.
Now add the following code in spec/requests/static_pages_spec.rb
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
end
end
at this stage i add the following code in sample_app/spec/spec_helper.rb file
config.include Capybara::DSL
so my file look like
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
config.include Capybara::DSL
end
i have app/views/static_pages/home.html.erb
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
and run the command
$ bundle exec rspec spec/requests/static_pages_spec.rb
it was giving the error
c:/wamp/www/rails_projects/sample_app/spec/spec_helper.rb:42:in `block in <top (
required)>': uninitialized constant Capybara (NameError)
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core.rb:120:in `configure'
from c:/wamp/www/rails_projects/sample_app/spec/spec_helper.rb:15:in `<t
op (required)>'
from c:/wamp/www/rails_projects/sample_app/spec/requests/static_pages_sp
ec.rb:1:in `require'
from c:/wamp/www/rails_projects/sample_app/spec/requests/static_pages_sp
ec.rb:1:in `<top (required)>'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/configuration.rb:896:in `load'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/configuration.rb:896:in `each'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/configuration.rb:896:in `load_spec_files'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/command_line.rb:22:in `run'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/runner.rb:80:in `run'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/runner.rb:17:in `block in autorun'
i have installed the capybara as per direction in git
step 1->gem install capybara
step 2->require 'capybara/rails' in sample_app/spec/spec_helper.rb
but still it was showing the load error of capybara rails
now i run the same command i.e $ bundle exec rspec spec/requests/static_pages_spec.rb
i got the following error
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-4.0.0/lib/act
ive_support/dependencies.rb:228:in `require': cannot load such file -- capybara/
rspec (LoadError)
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-
4.0.0/lib/active_support/dependencies.rb:228:in `block in require'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-
4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-
4.0.0/lib/active_support/dependencies.rb:228:in `require'
from c:/wamp/www/rails_projects/sample_app/spec/spec_helper.rb:8:in `<to
p (required)>'
from c:/wamp/www/rails_projects/sample_app/spec/requests/static_pages_sp
ec.rb:1:in `require'
from c:/wamp/www/rails_projects/sample_app/spec/requests/static_pages_sp
ec.rb:1:in `<top (required)>'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/configuration.rb:896:in `load'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/configuration.rb:896:in `each'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/configuration.rb:896:in `load_spec_files'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/command_line.rb:22:in `run'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/runner.rb:80:in `run'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.1
4.5/lib/rspec/core/runner.rb:17:in `block in autorun'
and my gemfile is
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
group :development, :test do
gem 'rspec-rails', ">= 2.0.0.beta"
end
gem 'rubyzip'
if i remove the config.include Capybara::DSL then i get fail error
Please give me right direction ...thanks in advance
Since you're using Bundler (i.e. you have a Gemfile), you need to make sure Capybara is in there too, otherwise your rails app won't know the gem is installed. Update your Gemfile to include capybara in the development/test environments:
group :development, :test do
gem 'rspec-rails', ">= 2.0.0.beta"
gem 'capybara'
end

Undefined method 'visit' for #<Class:XYZ> (NoMethodError) [rspec]

I'm getting the following error when I try to run an rspec test:
/srv/offerme/spec/requests/static_pages_spec.rb:13: undefined method `visit' for #<Class:0xb3436684> (NoMethodError)
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
from /srv/offerme/spec/requests/static_pages_spec.rb:12
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/dsl.rb:18:in `describe'
from /srv/offerme/spec/requests/static_pages_spec.rb:3
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load_spec_files'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `map'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load_spec_files'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:22:in `run'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:69:in `run'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in `autorun'
from /usr/local/bin/rspec:19
The file I get this error in looks like this (spec/requests/static_pages_spec.rb) :
require 'spec_helper'
describe "StaticPages" do
include Capybara::DSL
describe "GET /static_pages" do
it "works! (now write some real specs)" do
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
get static_pages_index_path
response.status.should be(200)
end
end
describe "Home page" do
visit 'static_pages/home'
page.should have_content('OfferMe')
end
end
My spec_helper.rb file looks like this:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
config.include Capybara::DSL
end
module ::RSpec::Core
class ExampleGroup
include Capybara::DSL
include Capybara::RSpecMatchers
end
end
And, finally, my Gemfile looks like this:
source 'https://rubygems.org'
gem 'rails', '3.2.7'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
group :development, :test do
gem 'sqlite3'
gem 'rspec-rails'
end
group :test do
gem 'capybara'
end
gem 'json'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.4'
gem 'coffee-rails', '~> 3.2.2'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '>= 1.0.3'
gem 'twitter-bootstrap-rails'
end
gem 'jquery-rails'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# gem 'jbuilder'
# Use unicorn as the app server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'ruby-debug'
gem 'therubyracer', '0.10.1'
gem 'libv8'
gem 'webrat'
I have seen this GitHub issue and have tried some of the workarounds as well as looked at some other StackOverflow questions pertaining to the similar issue (that's why some of the code looks redundant/hacky). However, none of them have worked yet. Please help!
UPDATE:
The problem is that you're calling visit outside of an it block, here:
describe "Home page" do
visit 'static_pages/home'
page.should have_content('OfferMe')
end
Wrap those middle lines in an it block:
describe "Home page" do
it "has a homepage" do
visit 'static_pages/home'
page.should have_content('OfferMe')
end
end
That should work.
ORIGINAL ANSWER:
I could be wrong, but I believe you have to include capybara in both the test and development sections of your Gemfile.
Try changing that section of your Gemfile to this:
group :development, :test do
gem 'sqlite3'
gem 'rspec-rails'
gem 'capybara'
end
This is an old post. I have had the same issue. I thought I share my solution.
In my Gemfile I have had
group :test do
gem 'capybara'
end
And the visit was in the right spot however I was getting the same error. so this is what I did. I ran the update on the Gem and all are good now.
bundle update capybara
Hope this help someone.

Resources