rails 3.2 capybara Capybara::ElementNotFound:Unable to find xpath "/html" - ruby-on-rails

I am trying to test my rails app with rspec 2.10.0 + capybara 1.1.2. Here is my test file
require 'spec_helper'
describe AdminPanelController do
describe "index" do
it "should have return code 200" do
visit '/admin'
page.should have_content "hello"
#response.status.should be(200)
end
end
end
And here are test result
Failure/Error: page.should have_content "hello"
Capybara::ElementNotFound:
Unable to find xpath "/html"
I google about this issue but find only information that webrat can be a problem however i do not have this gem installed. Thanks for any suggestions.

Wrong type of test. This looks like a controller test, which does tests with methods like get and post and is in the spec/controllers folder. Request specs, which use capybara, reside in spec/requests. Run $ rails generate scaffold SomeModel to see how they each should look.
If you understood the above but would still like to use capybara for your controller test, modify your describe block:
describe AdminPanelController, :type => :request do
...
end

Related

Feature test error - uninitialized constant FFaker::File

This is my feature spec test for creating a new post
require 'rails_helper'
require 'ffaker'
RSpec.feature 'Creating posts', :type => :feature do
scenario 'can create a new post' do
visit root_path
click_link 'New Post'
attach_file('Image', FFaker::File.file_name('spec/files', 'foo', 'jpg'))
fill_in 'Caption', with: 'Hello World! This is the first post!'
click_button 'Create Post'
expect(page).to have_content('Post was successfully created')
end
end
When I run the rspec command to run the test I get the following error
Failure/Error: attach_file('Image', FFaker::File.file_name('spec/files', 'foo', 'jpg'))
NameError:
uninitialized constant FFaker::File
How can I fix this?
Basically I htought to use ffaker for trying to upload an image instead of using a real image file. Is this supposed to work with ffaker, right?
The error tells you that Rails can't find FFaker::File module. FFaker gem reference tells me that there's actually no FFaker::File module in FFaker.
But there is one in Faker (Faker::File). And since FFaker is just refactored Faker I believe you can use Faker for your needs.

have_selector is failing test

I am having a problem including a should have_selector problem with rspec:
This is my code:
describe "GET 'home'" do
it "returns http success" do
get 'home'
expect(response).to be_success
end
it "should have the right title" do
should have_selector("title",
:content => "Ruby on Rails Tutorial Sample App | Home")
end
end
I have included the following at the top:
RSpec.describe PagesController, :type => :controller do
render_views
My html5 has the following:
<title>Ruby on Rails Tutorial Sample App | Home</title>
and I get an error message saying:
Failures:
1) PagesController GET 'home' should have the right title
Failure/Error: should have_selector("title",
expected #<PagesController:0x007fceef586a90> to respond to `has_selector?`
# ./spec/controllers/pages_controller_spec.rb:14:in `block (3 levels) in <top (required)>
Can someone help with that?
rspec -v
3.0.2
rails 4.1.1
thank you in advance.
Rspec 3 does not include the capybara matchers in controller specs by default. You can change this for an individual spec by doing
include Capybara::RSpecMatchers
Or, in your spec helper
config.include Capybara::RSpecMatchers, :type => :controller
Your next issue is that recent versions of capybara don't allow you to test for the presence of invisible elements by default, and the title element is considered to be invisible. You should use the have_title matcher instead.
Hollo rubyist pal!
I had this problem and that was because of two things, first I did not use capybara gem, and second have_selector only accepts one of :count, :minimum, :maximum, :between, :text, :visible, :exact, :match, :wait keys and does not understand :content.
I am sure you solved the problem, but for those who have started to learn Ruby on Rails recently and encountered such a problem I should say to eleminate it first put capybara gem in your Gemfile as below:
group :development, :test do
...
gem 'capybara'
end
and run command
bundle install
then to test that your view has a specific title, in pages_controller_spec.rb file write
describe "GET #home" do
...
it "should have the right title" do
get :home
expect(response.body).to have_selector('title', :text => 'Ruby on Rails rocks')
end
end
I hope it would help someone.
Cheerio!
Checking content is view spec...
And controller spec means checking response status/path, render page/partials, instance variable filters params.

Nesting scenarios with feature specs in Rspec Rails 3

I'm using Rspec Rails with Capybara for testing and I want to use the new feature spec in RSpec Rails 3 as they read more as customer tests and acceptance tests. However, one thing I find missing from the older (Describe/It) style is nesting. When trying to nest scenarios or use background inside any scenario block, I get an undefined method error. Is there anyway I could achieve nesting with feature specs to get something like this (from Michael Hartl's Ruby On Rails Tutorial:
describe "Authentication" do
subject { page }
describe "authorization" do
let(:user) { FactoryGirl.create(:user) }
describe "for non-signed in users" do
describe "when attempting to visit a protected page" do
before { visit edit_user_path(user) }
it "should redirect_to to the signin page" do
expect(page).to have_title('Sign in')
end
describe "after signing in" do
before do
valid_signin user, no_visit: true
end
it "should render the desired protected page" do
expect(page).to have_title('Edit user')
end
Or should I be thinking in a different way about integration tests ?
As described in https://www.relishapp.com/rspec/rspec-rails/docs/feature-specs/feature-spec, feature corresponds to describe and scenario corresponds to it. So, you can nest instances of feature, but you cannot nest a scenario within a scenario, just as you cannot nest an it within a it.
Nested feature with scenarios is available in Capybara version 2.2.1
In Gemfile include
gem "capybara", "~> 2.2.1"
and bundle install
As per official documentation of Capybara
feature is in fact just an alias for describe ..., :type => :feature,
background is an alias for before, scenario for it, and given/*given!*
aliases for let/*let!*, respectively.
Here is the original issue and later it was accepted and merged in version 2.2.1

why doesn't selenium see objects in my db?

given
require 'spec_helper'
describe "visit '/'" do
context "displays ", :driver => :selenium do
before :each do
FactoryGirl.create :restaurant, name: "John's Cafe"
visit '/'
end
context "content:" do
it "Restaurantly Spots!" do
expect(page).to have_content 'Restaurantly Spots!'
end
it "John's Cafe" do
expect(page).to have_content "John's Cafe"
end
end
end
end
capybara test using selenium is failing for 'have_content' "John's Cafe".
Is capybara not able to see db objects that were added to db in before block?
I'm certain that the test db has the object in it, but it isn't getting displayed on the view page, the view page is wired correctly in development, I'm more curious why this test won't pass. Thanks.
my test suite is configured based on avdi's blog post
http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/
A few things you could try:
a) Add the 'pry' gem to your gemfile and then you can add a binding.pry to your test code. For example:
it "John's Cafe" do
binding.pry
expect(page).to have_content "John's Cafe"
end
At this point when you run the test you will see a console (like irb or rails console) and have introspection to your application. So you can see if the content is in the database or not. For example:
Restaurant.last
That should give you a clue as to if the restaurant is getting saved to the database.
One question, does the first test pass? "Restaurantly Spots!"?
Let me know if this works or you have more questions.
Mike Riley

render_views returning NoMethodError

I'm currently going through the Ruby on Rails Tutorial by Michael Hartl, and in Section 3.2.2 of the tutorial, he talks about using RSpec to test for the titles of the page. My tests (which I wrote myself, following his tutorial) kept failing the title tests (it can find the tag, but the title is always a blank string), and some searching on Google told me I needed to include the function render_views to pass the tests. My problem is that no matter what I try, RSpec returns a NoMethodError for render_views. The things I've tried:
config.render_views in spec_helper.rb
describe "Static Pages" do
render_views
...
end in the spec file
describe "Static Pages" do
RSpec::Rails::ViewRendering.render_views
...
end in the spec file
All of them return a NoMethodError.
I'm not following his Gemfile exactly, so the versions of Ruby and the relevant gems are:
Ruby: 1.9.3-p125
rails: 3.2.9
rspec: 2.12.0
rspec-rails: 2.12.0
capycabra: 2.0.1
I'm working in RubyMine 4.5.4, on Windows 7. Running the test in a command prompt returns the same errors however. The spec file is static_pages_spec.rb and is located in spec/requests
The code in my static_pages_spec.rb:
require_relative '../spec_helper'
describe "Static Pages" do
#RSpec::Rails::ViewRendering.render_views, returns NoMethodError
#render_views, returns NameError
describe "Home Page" do
it "should have the h1 'Sample App'" do
visit '/static_pages/home'
page.should have_selector('h1', :text => "Sample App")
end
it "should have the title 'Home'" do
visit '/static_pages/home'
page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | Home")
end
end
describe "Help Page" do
it "should have the h1 'Help'" do
visit '/static_pages/help'
page.should have_selector('h1', :text => "Help")
end
it "should have the title 'Help'" do
visit '/static_pages/help'
page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | Help")
end
end
describe "About Page" do
it "should have the h1 'About Us'" do
visit '/static_pages/about'
page.should have_selector('h1', :text => "About Us")
end
it "should have the title 'About Us'" do
visit '/static_pages/about'
page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | About Us")
end
end
end
My spec_helper.rb is the spec_helper.rb automatically generated by creating running rails generate integration_test after creating the project with --no-test-framework, with these lines added in the config block:
# Fix NoMethodError for method 'visit'
config.include Capybara::DSL
config.include Capybara::RSpecMatchers
EDIT
After some testing, I've managed to get the tests to pass by changing the capybara version to 1.1.2, while still using rspec 2.12.0 and rspec-rails 2.12.0 and not using render_views. While I'm glad the tests are passing, it still doesn't solve the problem of the missing render_views method, which still crops up if I try to use it.
Well, after a bit more testing, the mystery of the missing render_views method is solved. To include the render views method, apparently config.include RSpec::Rails::ViewRendering in the spec_helper is needed before calling config.render_views in the spec_helper or render_views in the spec file.
Secondly, with regards to the tests passing or failing, the problem seems to be in Capybara itself. Apparently, it seems that the way the visit method is implemented in 1.1.2 and 2.0.1 is significantly different in what it returns; even after including render_views, the test still fails (without using config.include Capybara::RSpecMatchers). Using it seems to provide an insight into its implementation though; the failure message from Capybara on using the RSpecMatchers is that it is looking for CSS instead of HTML. I don't claim to fully understand why it fails, however, so if anyone can enlighten me as to why Capybara 2.0.1 causes the tests to fail while 1.1.2 allows the tests to pass, that would be great.
Capybara 2.0 requires all tests to be in spec/features, and not in spec/requests.
You can read all about it here.
I would suggest that you stick to the versions mentioned in Hartl's tutorial, atleast in your beginner days. Will help you get through the tutorial faster!

Resources