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!
Related
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.
I'm trying to run very simple RSpec tests following the Rails Tutorial and they surprisingly fail when they're not supposed to.
Fiona somewhere else suggested to move the file application.html.erb in app/views/layouts but mine is already there.
Zetetic suggested to add "render views" but I did and nothing changed.
The versions of the sw i'm using are as follows:
rvm 1.25.26
rspec 3.0.1
ruby 1.9.3p547
Rails 4.1.1
I get the following failure message:
$>rspec spec/requests/pages_spec.rb
F
Failures:
1) Pages Home page should have the h1 'Sample App'
Failure/Error: page.should have_selector('h1', :text => 'Sample App')
expected #has_selector?("h1", {:text=>"Sample App"}) to return true, got false
# ./spec/requests/pages_spec.rb:9:in `block (3 levels) in <top (required)>'
Deprecation Warnings:
Requiring `rspec/autorun` when running RSpec via the `rspec` command is deprecated. Called from /home/iammyr/.rvm/gems/ruby-1.9.3-p547/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `require'.
Using `should` from rspec-expectations' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` instead. Called from /home/iammyr/railsgirls-app/projects/galway_june2014/spec/requests/pages_spec.rb:9:in `block (3 levels) in <top (required)>'.
Failed examples:
rspec ./spec/requests/pages_spec.rb:7 # Pages Home page should have the h1 'Sample App'
One difference from the tutorial is that rather than generating those static pages with "rails generate static_pages" I did run "rails generate controller pages home help" but that shouldn't have to do with the rspec result, imho.
The files look as follow.
pages_spec.rb
require 'spec_helper'
describe "Pages" do
render_views
describe "Home page" do
it "should have the h1 'Sample App'" do
visit '/pages/home'
page.should have_selector('h1', :text => 'Sample App')
end
end
end
home.html.erb
<% provide(:title, 'Home') %>
<h1>Sample App</h1>
<p>
This is the home page.
</p>
Thanks a million to whomever would like to help me! thank you! ;)
You should first move the spec from requests to features folder see changes in capybara
I guess it will fix it (remember having a similar one)
If not debug it with pry and in Firefox with selenium driver
#Gemfile
gem 'pry-rails'
#spec
it "should have the h1 'Sample App'", :js => true do
visit '/pages/home'
binding.pry
page.should have_selector('h1', :text => 'Sample App')
end
Firefox should start and render the page
Then in debug mode check the capybara selector
page.all(:css, 'h1')
Perhaps you didn't define capybara or another driver. Then you try content instead of text:
page.should have_selector('h1', :content => 'Sample App')
I'm plodding through Mike Hartl's Rails Tutorial and in section 4.4 it appears that it had me change the rspec request file from the format of:
page.should have_selector('title', :text => "#{base_title}")
to
expect(page).to have_title("Ruby on Rails Tutorial Sample App")
I now get two undefined method errors where I use ".to have_title" and ".to_not have_title". I shutdown and restarted Webrick, Spork, and Guard just in case, but it still doesn't work.
Capybara version 1.1.2
Rspec version 2.11.1
Please let me know if any other info is needed.
Apparently the tutorial has changed recently.
Accessing the page through google cache reveals the original version (which works fine for me):
require 'spec_helper'
describe "Static pages" do
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 base title" do
visit '/static_pages/home'
page.should have_selector('title',
:text => "Ruby on Rails Tutorial Sample App")
end
it "should not have a custom page title" do
visit '/static_pages/home'
page.should_not have_selector('title', :text => '| Home')
end
end
.
.
.
end
May be the issue with your version. Go through https://github.com/jnicklas/capybara/issues/863
expect(first('title').native.text).to eq "my title"
I had this problem, I realised eventually I had switched to following the code for rails version 4, rather than 3.2.
to test what version of rails you are using, type the command
rails -v
Then on the tutorial page, use the navigator on the right to select the right rails version. Seems simple, but hopefully it will save grief for others with this issue.
I have this test from michael hartl book:
require 'spec_helper'
describe "Static pages" do
let(:base_title) { "Ruby on Rails Tutorial Sample App" }
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 => "#{base_title} | Home")
end
end
end
And the view:
<% provide(:title, 'Home') %>
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
When I run the test it says:
....
Finished in 1.91 seconds
4 examples, 0 failures
Randomized with seed 42247
.F...
Failures:
1) Static pages Home page should have the title 'Home'
Failure/Error: page.should have_selector('title', :text => "#{base_title} | Home")
expected #has_selector?("title", {:text=>"Ruby on Rails Tutorial Sample App | Home"}) to return true, got false
# ./spec/requests/static_pages_spec.rb:16:in `block (3 levels) in <top (required)>'
Finished in 1.91 seconds
5 examples, 1 failure
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:14 # Static pages Home page should have the title 'Home'
Randomized with seed 17491
But It should pass, because when I view the page in browser the title is: Ruby on Rails Tutorial Sample App | Sample App, which is correct!
Make sure you're using capybara 1.1.2 in your Gemfile. Starting from 2.0 capybara does not works for title testing (https://github.com/jnicklas/capybara/issues/844)
...
group :test do
gem 'capybara', '1.1.2'
end
For the time being, you should do what #dimuch suggests and make sure you specify the same Capybara version Michael Hartl uses in the tutorial (1.1.2).
If you want to upgrade to Capybara 2.0 in the future and keep your tests for titles, have a look at this StackOverflow answer for a guide to creating a RSpec matcher that will do what you're expecting.
Using capubara 2.0 you should use
page.should have_title("The title")
But in aint work if you dont add
<style type="text/css">head, head title { display: block }</style>
To your application.html
page.title # => "The title"
page.has_title?("The title") # => true
page.should have_title("The title")
I've been using the following and they have been posting green. I dropped have_selector and went with have_title.
it { should have_title( full_title('Sign up') ) }
-- and --
it { should have_title(user.name) }
This is with capybara 2.2.0.
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