URI::InvalidURIError while i am testing with rspec+capybara - ruby-on-rails

I am testing with RSpec+capybara, but i've got the error:
Failure/Error: page.should have_selector('title', :text => "YourPos | About")
expected #has_selector?("title", {:text=>"YourPos | About"}) to return true, got false
# ./spec/requests/static_pages_spec.rb:14:in `block (3 levels) in <top (required)>'
And my static_pages_spec.rb
require 'spec_helper'
describe "Static pages" do
describe "About page" do
it "should have the title 'About' " do
visit '/static_pages/about'
page.should have_selector('title', :text => "YourPos | About")
end
end
end
And my app/views/static_pages/about.html.erb
<!DOCTYPE html>
<html>
<head>
<title>YourPos | About</title>
</head>
<body>
<p>Find me in app/views/static_pages/home.html.erb</p>
</body>
</html>
Wish someone will help me, Thanks a lot.

Try changing
visit 'static_pages/about'
to
visit '/static_pages/about'

Related

how to check the presence of the element on the page?

please help solve the problem. i use rails4 + rspec + capybara.
my page has title-element:
<!DOCTYPE html>
<html lang="ru">
<head>
<title>I am learning Rails</title>
<link rel="stylesheet" media="all" href="/assets/albums.self855.css?body=1" data-turbolinks-track="true" />
......
.........
........
rspec test:
require 'spec_helper'
describe ImagesController, type: :controller do
describe "index action" do
it 'render title-element on root page' do
visit '/'
#page.should have_selector 'head title', :visible => false
page.should have_selector('head title',
:text => "Складик картинок")
end
end
end
i run in console:
rspec spec
but console displays follow error messages:
...F
Failures:
1) ImagesController index action render title-element on root page
Failure/Error: page.should have_selector('head title',
expected to find css "head title" with text "I am learning Rails" but there were no matches
# ./spec/controllers/images_controller_spec.rb:37:in `block (3 levels) in <top (required)>'
Finished in 0.94045 seconds (files took 1.95 seconds to load)
4 examples, 1 failure
Failed examples:
rspec ./spec/controllers/images_controller_spec.rb:30 # ImagesController index action render title-element on root page
The title element isn't displayed on the page so it's not found by capybaras normal finders, however there is a title matcher you can use
page.should have_title('your title')
Updated Answer:
Turns out that, there is already a have_title [Capybara RSpec matcher]
which you can use to solve your problem.
describe ImagesController, type: :controller do
describe "index action" do
it 'render title-element on root page' do
visit '/'
expect(page).to have_title "Складик картинок"
end
end
end

RoR , trouble with full_title

I am on Chapter 4 of Michael Hartl's RoR tutorial, and having some troubles with rspec and using the full_title helper function. In part 4.1 of the tutorial, I have the helper code as follows.
app/helpers/application_helper.rb
module ApplicationHelper
# Returns the full title on a per-page basis.
def full_title(page_title)
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
"#{base_title} | #{page_title}"
end
end
end
app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", media: "all",
"data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
spec/requests/static_pages_spec.rb
require 'spec_helper'
describe "Static pages" do
let(:base_title) {"Ruby on Rails Tutorial Sample App"}
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
it "should have the base title" do
visit '/static_pages/home'
expect(page).to have_title("Ruby on Rails Tutorial Sample App")
end
it "should not have a custom page title" do
visit '/static_pages/home'
expect(page).not_to have_title('| Home')
end
end
end
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 when I try to run test I get this:
gvyntyk#gvyntyk-r60:~/rails_projects/sample_app$ rspec spec/requests/static_pages_spec.rb
FFF
Failures:
1) Static pages Home page should not have a custom page title
Failure/Error: visit '/static_pages/home'
ActionView::Template::Error:
undefined local variable or method `base_title' for #<#<Class:0xb0ea7f8>:0xb0ea1cc>
# ./app/helpers/application_helper.rb:6:in `full_title'
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___36363065_93308240'
# ./spec/requests/static_pages_spec.rb:20:in `block (3 levels) in <top (required)>'
2) Static pages Home page should have the base title
Failure/Error: visit '/static_pages/home'
ActionView::Template::Error:
undefined local variable or method `base_title' for #<#<Class:0xb0ea7f8>:0x961e58c>
# ./app/helpers/application_helper.rb:6:in `full_title'
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___36363065_93308240'
# ./spec/requests/static_pages_spec.rb:15:in `block (3 levels) in <top (required)>'
3) Static pages Home page should have the content 'Sample App'
Failure/Error: visit '/static_pages/home'
ActionView::Template::Error:
undefined local variable or method `base_title' for #<#<Class:0xb0ea7f8>:0x9c13244>
# ./app/helpers/application_helper.rb:6:in `full_title'
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___36363065_93308240'
# ./spec/requests/static_pages_spec.rb:10:in `block (3 levels) in <top (required)>'
Finished in 1.23 seconds
3 examples, 3 failures
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:19 # Static pages Home page should not have a custom page title
rspec ./spec/requests/static_pages_spec.rb:14 # Static pages Home page should have the base title
rspec ./spec/requests/static_pages_spec.rb:9 # Static pages Home page should have the content 'Sample App'
Can anyone tell me what's going wrong here?
It feels like you've not posted the exact code for what the tests are running against. I'm not seeing how by what you've posted the code is even getting to full_title in the helper as you only have yeild(:title) in application.html.erb. Nothing is calling full_title.
Looking at Michaels example in the book you need to have full_title(yield(:title)) in the title tags removing what you've got posted there.

Hartl's Rails Tutorial Section 3.5

I've been working through Michael Hartl's Rails tutorial, and for some reason I've gotten stuck on the first exercise in section 3. I've checked and rechecked my code to ensure that it matches his, but I still get this error:
Failures:
1) Static pages Contact page should have the content 'Contact'
Failure/Error: expect(page).to have_content('Contact')
expected #has_content?("Contact") to return true, got false
# ./spec/requests/static_pages_spec.rb:48:in `block (3 levels) in <top (required)>'
2) Static pages Contact page should have the title 'Contact'
Failure/Error: expect(page).to have_title("Ruby on Rails Tutorial Sample App | Contact")
expected #has_title?("Ruby on Rails Tutorial Sample App | Contact") to return true, got false
# ./spec/requests/static_pages_spec.rb:53:in `block (3 levels) in <top (required)>'
Finished in 0.09624 seconds
8 examples, 2 failures
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:46 # Static pages Contact page should have the content 'Contact'
rspec ./spec/requests/static_pages_spec.rb:51 # Static pages Contact page should have the title 'Contact'
Here is my code
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
it "should have the title 'Home'" do
visit '/static_pages/home'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Home")
end
end
describe "Help page" do
it "should have the content 'Help'" do
visit '/static_pages/help'
expect(page).to have_content('Help')
end
it "should have the title 'Help'" do
visit '/static_pages/help'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Help")
end
end
describe "About page" do
it "should have the content 'About Us'" do
visit '/static_pages/about'
expect(page).to have_content('About Us')
end
it "should have the title 'About Us'" do
visit '/static_pages/about'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | About Us")
end
end
describe "Contact page" do
it "should have the content 'Contact'" do
visit '/static_pages/about'
expect(page).to have_content('Contact')
end
it "should have the title 'Contact'" do
visit '/static_pages/about'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Contact")
end
end
end
application.html.erb, which is saved under app/views/layouts
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title>
<%= stylesheet_link_tag "application", media: "all",
"data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
contact.html.erb
<% provide(:title, 'Contact') %>
<h1>Contact</h1>
<p>
Contact Ruby on Rails Tutorial about the sample app at the
contact page.
</p>
routes.rb
SampleApp::Application.routes.draw do
get "static_pages/home"
get "static_pages/help"
get "static_pages/about"
get "static_pages/contact"
end
static_pages_controller.rb
class StaticPagesController < ApplicationController
def home
end
def help
end
def about
end
def contact
end
end
With the limited information you're providing (I know it's hard to diagnose when you're a beginner, it gets easier). It looks like you're telling your test to check your about page and checking for content that only exists in your contact page.

Rspec let variable not working with have_title

I'm having trouble getting one of my Rspec tests to pass using Capybara 2.1.0
This works:
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the title 'Home'" do
visit '/static_pages/home'
page.should have_title("MyApp | Home")
end
end
end
This does not
require 'spec_helper'
describe "Static pages" do
let(:site_title) {"MyApp"}
describe "Home page" do
it "should have the title 'Home'" do
visit '/static_pages/home'
page.should have_title("#{site_title} | Home")
end
end
end
Any thoughts? I've been working on it for quite a while. Here is the error message that I receive.
1) Static pages Home page should haven the title
Failure/Error: page.should have_title("#{site_title} | Home")
expected #has_title?("MyApp | Home") to return true, got false
# ./spec/requests/static_pages_spec.rb:16:in `block (3 levels) in <top (required)>'
It seems you are missing the curly braces for #{site_title} or you need to use double quotes for your string to allow interpolation of variables. You have:
page.should have_title('#site_title | Home')
where it should be
page.should have_title("#{site_title} | Home")
Try this:
require 'spec_helper'
describe "Static pages" do
let(:site_title) {"MyApp"}
describe "Home page" do
it "should have the title 'Home'" do
visit '/static_pages/home'
page.should have_selector('title', text: "#{site_title} | Home")
end
end
end

Ruby on Rails embedded ruby generating errors

I am working my way through Ruby on Rails 2nd Ed., Hartl. In section 3.3.3 my tests failed after I removed some static text with some embedded Ruby. The test was passing previously no problem.
rspec
require 'spec_helper'
describe "Home 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 title 'Home'" do
visit '/static_pages/home'
page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | Home")
end
end
end
home.html.erb
<% provide (:title, 'Home') %>
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title>
</head>
<body>
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
</body>
</html>
error from console
C:\Sites\rails_projects\sample_app>bundle exec rspec spec/requests/static_pages_spec.rb
?[31mF?[0m?[31mF?[0m?[32m.?[0m?[32m.?[0m?[32m.?[0m?[32m.?[0m
Failures:
1) Home pages Home page should have the h1 'Sample App'
?[31mFailure/Error:?[0m ?[31mvisit '/static_pages/home'?[0m
?[31mActionView::Template::Error:?[0m
? [31mC:/Sites/rails_projects/sample_app/app/views/static_pages/home.html.erb:1: syntax error,
unexpected ',', expecting ')'?[0m
?[31m...putBuffer.new; provide (:title, ' Home') ?[0m
?[31m... ^?[0m
? [31mC:/Sites/rails_projects/sample_app/app/views/static_pages/home.html.erb:1: syntax error,
unexpected ')', expecting keyword_end?[0m
?[31m....new; provide (:title, ' Home') ?[0m
?[31m... ^?[0m
?[36m # <internal:prelude>:10:in `synchronize'?[0m
?[36m # ./spec/requests/static_pages_spec.rb:7:in `block (3 levels) in <top (required)>'?[0m
2) Home pages Home page should have the title 'Home'
?[31mFailure/Error:?[0m ?[31mvisit '/static_pages/home'?[0m
?[31mActionView::Template::Error:?[0m
? [31mC:/Sites/rails_projects/sample_app/app/views/static_pages/home.html.erb:1: syntax error,
unexpected ',', expecting ')'?[0m
?[31m...putBuffer.new; provide (:title, ' Home') ?[0m
?[31m... ^?[0m
? [31mC:/Sites/rails_projects/sample_app/app/views/static_pages/home.html.erb:1: syntax error,
unexpected ')', expecting keyword_end?[0m
?[31m....new; provide (:title, ' Home') ?[0m
?[31m... ^?[0m
?[36m # <internal:prelude>:10:in `synchronize'?[0m
?[36m # ./spec/requests/static_pages_spec.rb:12:in `block (3 levels) in <top (required)>'?[0m
Finished in 0.51562 seconds
?[31m6 examples, 2 failures?[0m
Failed examples:
?[31mrspec ./spec/requests/static_pages_spec.rb:6?[0m ?[36m# Home pages Home page should have the h1 'Sample App'?[0m
?[31mrspec ./spec/requests/static_pages_spec.rb:11?[0m ?[36m# Home pages Home page should have the title 'Home'?[0m
Any help is appreciated
I think you need to remove the whitespace prior to the parenthesis
provide (:title, 'Home')
should be
provide(:title, 'Home')

Resources