I am trying to run a simple test using rspec and capybara:
describe "Create" do
visit new_client_path
page.should have_selector('h1', text: "New Client")
end
However i get error:
undefined local variable or method `visit'
And if i remove the visit line i get this error:
undefined local variable or method `page'
I have included the DSL in the spec_helper file.
what is the problem?
Thanks
It seems that you simply forgot to put your test inside the it block:
describe "Create" do
it "does something" do
visit new_client_path
page.should have_selector('h1', text: "New Client")
end
end
Assuming you are using Rails 3 and RSpec, have you placed your specs in features instead of requests? You shouldn't need to include the DSL if you are using features directory.
Also check that you have latest version of the gems.
From https://github.com/jnicklas/capybara:
Using Capybara with RSpec
Load RSpec 2.x support by adding the following line (typically to your
spec_helper.rb file):
require 'capybara/rspec' If you are using Rails, put your Capybara
specs in spec/features.
Related
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 am using rspec to validate if a string exist in a page. I am new to rails and following tutorial http://ruby.railstutorial.org/chapters/static-pages#top
Is there anything I am missing?
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
page.should have_content('Sample App')
end
end
end
In my home page app/views/static_pages/home.html.erb I have following code.
<h1>Sample App</h1>
Command I am using to validate the condition.
bundle exec rspec spec/requests/static_pages_spec.rb
Test is failing don't know why.
←[31mF←[0m
Failures:
1) Static pages Home page should have the content 'Sample App'
←[31mFailure/Error:←[0m ←[31mvisit '/static_pages/home'←[0m
←[31mNoMethodError←[0m:
←[31mundefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::
Nested_1:0x4067a60>←[0m
←[36m # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top(required)>'←[0m
Finished in 0.023 seconds
←[31m1 example, 1 failure←[0m
Failed examples:
←[31mrspec ./spec/requests/static_pages_spec.rb:7←[0m ←[36m# Static pages Home page should have the content 'Sample App'←[0m
Randomized with seed 44188
Going by what the tutorial has you do and without your Gemfile to double check, I believe you are experiencing a conflict between Capybara 2.0 and RSpec on the visit keyword. You have two options for a solution (don't need to do both):
Option 1: Move your test from the spec/requests folder into the spec/features folder.
or
Option 2: Use the keyword feature instead of describe.
This conflict is caused by a change to Capybara noted at the top of this gist (second paragraph): https://gist.github.com/jnicklas/3980553. Quoted:
Notably, we changed the :type that Capybara assumes your specs run under in RSpec to :feature (previously it was :request). The latest release of spec/features. Alternatively you can use the Capybara Feature DSL (feature instead of describe), which should work without any additional tweaking. If you see errors like undefined method visit, then you're probably encountering this issue. If you're including modules into :request specs, you will probably need to change that to :feature.
Is there any way within an RSpec tests, by convention or code, to have rails start before tests run? I'm trying to setup a testing framework for selenium tests that use chrome, and now I'm only hindered by my lack of a running server.
require 'spec_helper'
describe 'The first tab' do
before(:each) do
#driver = Selenium::WebDriver.for :chrome
end
it 'Shows the list' do
#driver.navigate.to 'index.html'
end
end
I'm new to RSpec, so I'm not sure how I would create a suite of tests that all ran while a rails server was running.
You should be using Capybara to test this stuff instead. It uses selenium-webdriver internally to provide JavaScript testing support.
with Capybara, you put this test in either the spec/integration or spec/requests folder and write it like this:
require 'spec_helper'
describe 'The first tab' do
it "shows the list", :js => true do
visit list_path
end
end
By putting :js => true after the example's name Capybara will know to run this as a JavaScript test.
Just run rails server and kill the process went tests complete.