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
Related
I put a bootstrap navbar in my layout and want to test whether all the links are correct.
require 'rails_helper'
describe "layouts/application.html.erb" do
it "should have the right links in navbar" do
render
expect(page).to have_link('Home', href: "/home/index")
expect(page).to have_link('Games', href: "/games/index")
expect(page).to have_link('Programming', href: "/programming/index")
expect(page).to have_link('Bananenmannfrau', href: "/pages/about")
end
end
This just returns error:
layouts/application.html.erb should have the right links in navbar
Failure/Error: render
NameError:
undefined local variable or method `render' for #<RSpec::ExampleGroups::LayoutsApplicationHtmlErb:0x00000005a8f750>
# ./spec/layouts/application.html.erb_spec.rb:8:in `block (2 levels) in <top (required)>'
Is this even the correct way of testing it and when yes what am I missing?
You should use capybara gem for testing front end!
Capybara
Then you will be able to use it like:
require 'rails_helper'
describe "layouts/application.html.erb" do
it "should have the right links in navbar" do
visit root_path
within(".nav-bar-selector") do
expect(page).to have_content('Home')
expect(page).to have_content('Games')
expect(page).to have_content('Programming')
expect(page).to have_content('Bananenmannfrau')
end
end
end
I have set up guard following Michael's RoR Tutorials and intentionally wrote a test (on contact page title) so it fails. But Guard/RSpec tells me it passed and I am confused what's going on. This is my static_pages_spec.rb file:
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the content 'Welcome to the PWr App'" do
visit '/static_pages/home'
expect(page).to have_content('Welcome to the PWr App')
end
it "should have the title 'Home'" do
visit '/static_pages/home'
expect(page).to have_title("PWr | 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 title 'Help'" do
visit '/static_pages/help'
expect(page).to have_title("PWr | Help")
end
end
describe "About page" do
it "should have the content 'About me'" do
visit '/static_pages/about'
expect(page).to have_content('About Me')
end
it "should have title 'About Me'" do
visit '/static_pages/about'
expect(page).to have_title("PWr | About")
end
end
describe "Contact page" do
it "should have the content 'Contact'" do
visit '/static_pages/contact'
expect(page).to have_content('Contact')
end
it "should have title 'Contact'" do
visit '/static_pages/contact' do
expect(page).to have_title("FAIL")
end
end
end
end
And this is my contact.html.erb:
<% provide(:title, 'Contact') %>
<h1>Contact</h1>
<p1>
If you need to contact me just call the number below: </br>
+48 737823884
</p>
And results from my terminal:
18:43:57 - INFO - Running: spec/requests/static_pages_spec.rb
........
Finished in 0.08689 seconds
8 examples, 0 failures
Randomized with seed 55897
[1] guard(main)>
As you can see, in the spec file close to the end I have expect(page).to have_title("FAIL") and in the contact page html/erb I clearly have <% provide(:title, 'Contact') %> but the test passes. Why is this? What am I doing wrong?
The problem is that you are passing your expectation as a block to the visit method - ie notice the extra do-end. I do not believe that visit uses blocks, so basically that bit of code gets ignored.
it "should have title 'Contact'" do
visit '/static_pages/contact' do
expect(page).to have_title("FAIL")
end
end
Your spec should behave as expected if you remove the block.
it "should have title 'Contact'" do
visit '/static_pages/contact'
expect(page).to have_title("FAIL")
end
Hello I am having an issue with railstutorial.
I have the test file features/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'
page.should have_conent('Sample App')
end
end
end
when I run bundle exec rspec spec/features/static_pages_spec.rb
I get the following error:
Failures:
1) Static pages Home page Should have the content 'Sample App'
Failure/Error: page.should have_conent('Sample App')
NoMethodError:
undefined method `has_conent?' for #<Capybara::Session>
# ./spec/features/static_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
Finished in 0.05262 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/features/static_pages_spec.rb:6 # Static pages Home page Should have the content 'Sample App'
Randomized with seed 49777
I tried to add in the spec_helper.rb config.include Capybara::DSL but it gives me the same error.
Just a typo:
page.should have_conent('Sample App')
should be
page.should have_content('Sample App')
There is a typo. You are missing a t in your content
page.should have_content('Sample App')
I'm on the chapter 5 exercises for Michael Hartl's Rails Tutorial and am trying to wrap my head around how Rails/Rspec is testing a helper method full_title in app/helpers/application_helper.rb. All of my tests are in spec/requests/static_pages_spec.rb and within them, I'm calling full_title to cut down on code bloat.
So, in order to test the original full_title I create a test in spec/helpers/application_helpers_spec.rb and include it via spec/support/utilities.rb. The code is passing, but I want to understand the process (order to operations) to what is going on. Thank you.
Can I think of it like this?
Rspec begins to run static_pages_spec.rb (including utilities.rb)
Rspec sees the full_title method in static_pages_spec.rb
Rspec begins to run application_helper_spec.rb
Rspec sees describe "full_title" do in application_helper_spec.rb
Rspec looks up original full_title method and finishes test for application_helper_spec.rb
Rspec finishes tests in static_pages_spec.rb, iterating through above process whenfull_title` is called.
static_pages_spec.rb
require 'spec_helper'
describe "Static pages" do
subject { page }
shared_examples_for "all static pages" do
it { should have_selector('h1', text: heading) }
it { should have_selector('title', text: full_title(page_title)) }
end
describe "Home page" do
before { visit root_path }
let(:heading) { 'Sample App' }
let(:page_title) { '' }
it_should_behave_like "all static pages"
it { should_not have_selector 'title', text: '| Home' }
end
describe "Help page" do
before { visit help_path }
let(:heading) { 'Help' }
let(:page_title) { 'Help' }
it_should_behave_like "all static pages"
end
describe "About page" do
before { visit about_path }
let(:heading) { 'About' }
let(:page_title) { 'About Us' }
it_should_behave_like "all static pages"
end
describe "Contact page" do
before { visit contact_path }
let(:heading) { 'Contact' }
let(:page_title) { 'Contact' }
it_should_behave_like "all static pages"
end
it "should have the right links on the layout" do
visit root_path
click_link "About"
page.should have_selector 'title', text: full_title('About Us')
click_link "Help"
page.should have_selector 'title', text: full_title('Help')
click_link "Contact"
page.should have_selector 'title', text: full_title('Contact')
click_link "Home"
page.should have_selector 'title', text: full_title('')
click_link "Sign up now!"
page.should have_selector 'title', text: full_title('Sign up')
click_link "sample app"
page.should_not have_selector 'title', text: full_title('| Home')
end
end
application_helper_spec.rb
require 'spec_helper'
describe ApplicationHelper do
describe "full_title" do
it "should include the page title" do
full_title("foo").should =~ /foo/
end
it "should include the base title" do
full_title("foo").should =~ /^Ruby on Rails Tutorial Sample App/
end
it "should not include a bar for the home page" do
full_title("").should_not =~ /\|/
end
end
end
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
Think about it this way:
The 'full_title' called in static_pages_spec.rb (including utilities.rb) is running the 'full_title' method described in application_helper.rb.
The application_helper_spec.rb is validating the string/value (page_title) passed through full_title.
If I'm not mistaken, it does this each time the full_title method is called in your tests.
http://ruby.railstutorial.org/chapters/static-pages#code:pages_controller_spec_title
My code is as follows. When uncommented the test wont run. Below is the commented version of the code that allows the defined test to initiate. I don't know how to format this question with ruby syntax color. Dont yell at 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 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
# describe 'Contact' do
# it "Should have the h1 'Contact'" do
# visit "static_pages/contact"
# page.should have_selector('h1', :text => 'Contact')
# end
# it "should have the title 'Contact'"
# visit "static_pages/contact"
# page.should have_selector('title',
# :text => "Ruby on Rails Tutorial Sample App | Contact")
# end
# end
end
I guess you forgot do at the end of this line:
it "should have the title 'Contact'"
^