Hi I am working on Daniel Kehoe's Rspec Tutorial. Everything went fine until I created the file spec/features/visitor/home_page_spec.rb . When I put the following test code in it
# Feature: Home Page
# As a visitor
# I want to visit a home page
# So I can learn more about the website
feature 'Home Page' do
# Scenario: Visit the Home Page
# Given I am a visitor
# When I visit the home page
# Then I should see "Welcome"
scenario 'Visit the Home Page' do
visit root_path
expect(page).to have_content 'Welcome'
end
end
and run rspec spec/features/visitor/home_page_spec.rb from the terminal, I got the error below. I get that the method was not defined but Daniel's tutorial just tells you to put the code in the folder above. Guess I am missing something. Thank you
rails-bootstrap/spec/features/visitor/home_page_spec.rb:7:in `<top (required)>':
undefined method `feature' for main:Object (NoMethodError)
What's in your .rspec file in the project directory? You should have:
--color
--format documentation
--require spec_helper
--require rails_helper
If the .rspec file is set up properly, you will not need additional require statements in your spec files.
The only way I get rake test to run is to add the below require to test/integration/home_page_test.rb
require 'minitest/rails/capybara'
This line is already in test/test_helper.rb so I'm not sure why it isn't picking it up. I also tried adding the .rspec file with the options suggested by Daniel. If nothing else works, try that.
Related
I am trying to use the Spree 2.3 route helpers in Rspec 3.0. In the main app, I can access them by prefixing spree., like so:
spree.admin_login_path => 'spree/admin/user_sessions#new'
However I can't seem to access them in Rspec.
#rspec error
undefined local variable or method `spree_admin_login_path'
I've found reference to including the helpers in the rails_helper file, but this throws an error
# app/spec/rails_helper.rb
RSpec.configure do |config|
config.include Spree::Core::UrlHelpers
end
# configuring the above results in the following
app/spec/rails_helper.rb:21:in `block in <top (required)>': uninitialized constant Spree (NameError)
How do I access the spree routes given in $ rake routes in my tests?
After digging through the Spree code I was able to put together this setup for my rails_helper file that lets me use spree routes such as spree.admin_login_path in my spec files:
# app/spec/rails_helper.rb
require 'spree/testing_support/url_helpers'
RSpec.configure do |config|
config.include Spree::TestingSupport::UrlHelpers
end
I'm sure there's a smoother way to include all of Spree's test helpers, and I'd love to hear about it from someone who knows.
My java web application is running on tomcat at http://localhost:8080/
Writing my first spec, home_spec:
require 'spec_helper'
describe "home" do
it "should render the home page" do
visit "/"
page.should have_content("hello world")
end
end
And running:
rspec
I get:
F
Failures:
1) home should render the home page
Failure/Error: visit "/"
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x242870b7>
# ./spec/home/home_spec.rb:7:in `(root)'
Finished in 0.012 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/home/home_spec.rb:6 # home should render the home page
Shouldn't this work because I have included capybara in the spec_helper?
How will it know to visit the correct url? what if my url is localhost:3030 or localhost:8080?
My gemfile:
source 'http://rubygems.org'
gem "activerecord"
gem "rspec"
gem "capybara"
gem "activerecord-jdbcmysql-adapter"
My spec_helper:
require 'capybara/rspec'
Regarding to rspec issues (https://github.com/rspec/rspec-rails/issues/360)
you should put
config.include Capybara::DSL
in spec_helper.rb, inside the config block.
The default directory that Capybara::RSpec now looks at to include the Capybara::DSL and Capybara::RSpecMatchers is changed from requests to features.
After I renamed my requests directory to features I got the matcher and DSL methods available again without having to explicitly include them.
See the following commit
Also make sure your tests are in the /spec/features directory. According to rspec-rails and capybara 2.0, Capybara v2 and higher will not be available by default in RSpec request specs. They suggest to "...move any tests that use capybara from spec/requests to spec/features."
By default the capybara DSL is included automatically if the file is in spec/requests, spec/integration or if the example group has :type => :request.
Because your file is in spec/home the capybara helpers aren't being included. You can either conform to one of the patterns above or adding include Capybara::DSL should also do the trick (you might also need to replicate some of the before(:each) stuff that would be setup.)
First check it out
If you are not success,
Add this code your end of the your spec helper actually out of the RSpec.configure block as well
module ::RSpec::Core
class ExampleGroup
include Capybara::DSL
include Capybara::RSpecMatchers
end
end
1) Add to ‘rails_helper’ config:
config.include Capybara::DSL
config.include Capybara::RSpecMatchers
And comment out the `require 'spec_helper'` line.
2) Add to 'spec_helper':
require 'rails_helper'
I am getting a strange issue when using Guard to run my specs.
I am running a feature spec that uses the Capybara "feature" / "scenario" syntax. I am also using Spring.
Everything works fine if I run rspec spec or spring rspec in the console or rspec in the Guard shell. But, when the watched specs get run automatically by Guard, I get the following error:
/spec/features/navigation_spec.rb:1:in <top (required)>': undefined methodfeature' for main:Object (NoMethodError)
Why is it not picking up the Capybara syntax only in this specific context?
Here is the relevant code:
GuardFile
guard :rspec, :spring => true do
watch(%r{^spec/.+_spec\.rb$})
end
spec/features/navigation_spec.rb
feature "navigation" do
context "When on the home page" do
before { visit "/" }
scenario "I should see the navigation header" do
expect(page).to have_selector("div.navigation")
end
end
end
spec/spec_helper.rb
require 'capybara/rspec'
For anyone who may run into a similar issue in the future, I forgot to include require 'spec_helper' in the feature spec (like an idiot).
In Rails 4, make sure that you have included 'rails_helper' instead of 'spec_helper' on top of your specfile:
require 'rails_helper'
feature "Some Feature", :type => :feature do
..
end
And also make sure that config.disable_monkey_patching! is commented out or removed. Otherwise you will encounter problems when running your feature specs.
require 'capybara/rspec'
RSpec.configure do |config|
..
# config.disable_monkey_patching!
..
end
If you have created a .rspec file inside your project dir, also make sure to to change spec_helper to rails_helper there as well.
How are you invoking guard? It sounds like you might need to do bundle exec guard to kick things off. It could also be running under the wrong environment (unlikely, but worth a look).
My java web application is running on tomcat at http://localhost:8080/
Writing my first spec, home_spec:
require 'spec_helper'
describe "home" do
it "should render the home page" do
visit "/"
page.should have_content("hello world")
end
end
And running:
rspec
I get:
F
Failures:
1) home should render the home page
Failure/Error: visit "/"
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x242870b7>
# ./spec/home/home_spec.rb:7:in `(root)'
Finished in 0.012 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/home/home_spec.rb:6 # home should render the home page
Shouldn't this work because I have included capybara in the spec_helper?
How will it know to visit the correct url? what if my url is localhost:3030 or localhost:8080?
My gemfile:
source 'http://rubygems.org'
gem "activerecord"
gem "rspec"
gem "capybara"
gem "activerecord-jdbcmysql-adapter"
My spec_helper:
require 'capybara/rspec'
Regarding to rspec issues (https://github.com/rspec/rspec-rails/issues/360)
you should put
config.include Capybara::DSL
in spec_helper.rb, inside the config block.
The default directory that Capybara::RSpec now looks at to include the Capybara::DSL and Capybara::RSpecMatchers is changed from requests to features.
After I renamed my requests directory to features I got the matcher and DSL methods available again without having to explicitly include them.
See the following commit
Also make sure your tests are in the /spec/features directory. According to rspec-rails and capybara 2.0, Capybara v2 and higher will not be available by default in RSpec request specs. They suggest to "...move any tests that use capybara from spec/requests to spec/features."
By default the capybara DSL is included automatically if the file is in spec/requests, spec/integration or if the example group has :type => :request.
Because your file is in spec/home the capybara helpers aren't being included. You can either conform to one of the patterns above or adding include Capybara::DSL should also do the trick (you might also need to replicate some of the before(:each) stuff that would be setup.)
First check it out
If you are not success,
Add this code your end of the your spec helper actually out of the RSpec.configure block as well
module ::RSpec::Core
class ExampleGroup
include Capybara::DSL
include Capybara::RSpecMatchers
end
end
1) Add to ‘rails_helper’ config:
config.include Capybara::DSL
config.include Capybara::RSpecMatchers
And comment out the `require 'spec_helper'` line.
2) Add to 'spec_helper':
require 'rails_helper'
I'm trying to learn RoR following this tutorial and I'm currently in chapter 3. The tutorial works fine if I follow it line-by-line. However, the commands used in the tutorial suppress generation of default tests. When I try to keep them and possibly use them in my project, I always hit a wall somewhere.
Could you please tell me what I'm doing wrong?
$ rails new myproject
$ cd myproject/
$ echo "gem 'rspec'" >> Gemfile
$ echo "gem 'rspec-rails'" >> Gemfile
$ echo "gem 'capybara'" >> Gemfile
$ bundle install
$ bundle --binstubs
$ rails generate rspec:install
$ rails generate controller StaticPages home help about
Then I edit the spec/views/static_pages/home.html.erb_spec.rb file, to test whether capybara works:
require 'spec_helper'
#require 'capybara'
#require 'capybara/rails'
#require 'capybara/rspec'
describe "static_pages/home.html.erb" do
it 'should have a right title' do
visit '/static_pages/home'
page.should have_selector('title', :text => 'Home')
end
end
Running bin/rspec at this point, obviously, ends up with a failure. Well, a failure could have been expected. The reason for one of these failures is more alarming, though:
1) static_pages/home.html.erb should have a right title
Failure/Error: visit '/static_pages/home'
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_5:0x00000003dfd268>
# ./spec/views/static_pages/home.html.erb_spec.rb:7:in `block (2 levels) in <top (required)>'
The visit method, which AFAIK is part of Capybara, has not been found. Uncommenting the three extra requires in home.html.erb_spec.rb does not change anything in the result.
Any ideas what I'm doing wrong? Or what I should do better?
Rails version: 3.2.6
Put your test in requests directory instead of views.