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')
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 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'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'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!