I am newbie in rspec test.
I have a file /spec/models/class_spec.rb. The content of the file is
require 'rails_helper'
RSpec.describe Class, :type => :model do
#pending "add some examples to (or delete) #{__FILE__}"
before(:each) do
#my_class=Factory.create(:class, params)
end
end
I also have a /spec/rails_helper file. When I run rspec in command line
command : rspec spec/models/class_spec.rb
I gets the following error
/usr/local/rvm/gems/ruby-2.3.1/gems/airbrake-ruby-1.4.4/lib/airbrake-ruby.rb:102:in `configure': the 'default' notifier was already configured (Airbrake::Error)
from /home/akhil/test-prjct/ver3/this_prjct/config/initializers/errbit.rb:1:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
from /usr/local/rvm/gems/ruby-2.3.1/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
Can someone tell me about issue and also give a link to a tutorial for learning to write test codes using rspec. Thanks in advance
My gem list is
group :development, :test do
gem "rails-erd"
gem 'letter_opener'
gem 'spring'
gem 'faker'
gem 'pry-rails'
gem 'pry-byebug'
gem 'rspec-rails'
gem 'factory_girl_rails'
end
errbit.rb file has following content
Airbrake.configure do |config|
config.host = 'https://192.168.2.143:3000'
config.project_id = -1
config.project_key = '76068df1155420d2658i35a75o95gk26'
config.environment = Rails.env
config.ignore_environments = %w(development test)
end
Related
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
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
When running my rails 4 rspec suite via rake everything works correctly but when attempting to run rake simplecov I get hundreds of failures all w/ a NoMethodError something like:
1) Answer Validations
Failure/Error: it { should validate_presence_of :text }
NoMethodError:
undefined method `validate_presence_of' for #<RSpec::Core::ExampleGroup::Nested_16::Nested_1:0x007faf9fb5b7c8>
# ./spec/models/answer_spec.rb:12:in `block (3 levels) in <top (required)>'
Any clues as to why this is happening? I should also mention that I'm testing using sqlite3 :memory:.
The shoulda libraries aren't being included in your simplecov run.
Is simplecov run in a different environment than :test perhaps? That may make the shoulda gem not be loaded if it's only in the :test group in your Gemfile.
How are you including simplecov in your spec_helper? Something like this?
require 'simplecov'
require 'simplecov-rcov'
SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
SimpleCov.start 'rails' if ENV['COVERAGE']
As the very first lines of your spec_helper.rb?
Something like this in your Gemfile?
group :test, :development do
...
gem 'rspec-rails', '~> 2.0'
gem 'shoulda-matchers'
end
group :test do
gem 'simplecov'
gem 'simplecov-rcov'
end
And executing it like so?
$ COVERAGE=true bundle exec rake spec
This recipe is working very well for me in many projects. The RCov formatter may not be important depending on your purposes.
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.
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.