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')
Related
I'm doing the rails tutorial from Michael Hartl, and I can't get the test in chapter 3 to pass.
The static_pages.rb file:
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
end
end
The home.html.erb file:
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
And this is the result of the test:
An error occurred in an after hook
ArgumentError: rack-test requires a rack application, but none was given
occurred at C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/capybara-2.1.
0/lib/capybara/rack_test/driver.rb:16:in `initialize'
←[31mF←[0m
Failures:
1) Static pages Home page should have the content 'Sample App'
←[31mFailure/Error:←[0m ←[31mvisit '/static_pages/home'←[0m
←[31mArgumentError←[0m:
←[31mrack-test requires a rack application, but none was given←[0m
←[36m # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top
(required)>'←[0m
Finished in 0.001 seconds
←[31m1 example, 1 failure←[0m
Failed examples:
←[31mrspec ./spec/requests/static_pages_spec.rb:7←[0m ←[36m# Static pages Home p
age should have the content 'Sample App'←[0m
My current version of Capybara is 2.1.0 but I tried changing version to 1.1.2 and bundle update, but didn't work aswell.
Also tried different syntax for the test.
page.should have_title("Sample App")
page.should have_selector('h1', :text => 'Sample App')
page.should have_selector('title', text: 'Sample App')
My system is Windows 7, and my rails version is 4.0.8 and Ruby version is 1.9.3.
Tried searching around for the "rack-test requires a rack application, but none was given" error, and it seems I somehow need to change so that rack is not the default driver.
Well this is how my config file looks like.
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'capybara/rspec'
RSpec.configure do |config|
config.include Capybara::DSL
end
I am following Michael Hartls tutorial on rspec and I am getting this error
bundle exec rspec spec/requests/static_pages_spec.rb
F
Failures:
1) Static pages Home page should have the content 'Sample App'
Failure/Error: visit '/static_pages/home'
NoMethodError:
undefined method `visit' for #<RSpec::ExampleGroups::StaticPages::HomePage:0x007fdddbdf6a90>
# ./spec/requests/static_pages_spec.rb:6:in `block (3 levels) in <top (required)>'
Finished in 0.00052 seconds (files took 0.1562 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:5 # Static pages Home page should have the content 'Sample App'
for the rspec test.
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
I don't believe that the reason I am getting this error is the test case is failing but rather this:
NoMethodError:
undefined method `visit' for #<RSpec:....
I am not sure why there is an undefined method visit
maybe you just missed:
config.include Capybara::DSL
also you could look to the same qestion here
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
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