I have the following helper method (app/helpers/application_helper.rb):
module ApplicationHelper
#Return a title on a per-page basis
def title
base_title = "Ruby on Rails Tutorial Sample App"
if #title.nil?
base_title
else
"#{base_title} | #{#title}"
end
end
end
and here's the erb ( app/views/layouts/application.html.erb):
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<%= csrf_meta_tag %>
</head>
<body>
<%= yield %>
</body>
</html>
I ran an rspec test to see if this helper method works and it seems that it can't find title.
Here's the error message:
Failures:
1) PagesController GET 'home' should be successful
Failure/Error: get 'home'
ActionView::Template::Error:
undefined local variable or method `title' for #<#<Class:0x991ecb4>:0x991315c>
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb__248109341_80250010__979063050'
# ./spec/controllers/pages_controller_spec.rb:8:in `block (3 levels) in <top (required)>'
2) PagesController GET 'home' should have the right title
Failure/Error: get 'home'
ActionView::Template::Error:
undefined local variable or method `title' for #<#<Class:0x991ecb4>:0x9d7d094>
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb__248109341_82566280__979063050'
# ./spec/controllers/pages_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
Can anyone tell me what I did wrong?
UPDATE:
I included the helper by doing the following:
describe PagesController do
include ApplicationHelper
render_views
describe "GET 'home'" do
it "should be successful" do
get 'home'
response.should be_success
end
it "should have the right title" do
get 'home'
response.should have_selector("title",
:content => "Ruby on Rails Tutorial Sample App | Home")
end
end
//and some more
However I am still getting the same error
In your views, the helpers are not included by default.
You can mock out the helper methods using the template object:
template.should_receive(:title).and_return("Title")
You can then test your helpers separately.
Alternatively you can include your helpers in your view spec by simply doing:
include ApplicationHelper
EDIT
describe PagesController do
include ApplicationHelper
describe "GET 'home'" do
it "should be successful" do
controller.template.should_receive(:title).and_return("Title")
get 'home'
response.should be_success
end
end
end
Related
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.
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.
I am trying to dry up my testing and would like to check routes. app.send("home_path") returns "/home" in the console but when I run it in tests it comes back as an undefined method.
Any ideas?
1) StaticPages Check paths should have content Home
Failure/Error: visit app.send("#{term.downcase}_path")
NoMethodError:
undefined method `home_path' for #<test::Application:0x007f917a48e078>
# ./spec/requests/static_pages_spec.rb:18:in `block (4 levels) in <top (required)>'
["Home", "About"].each do |term|
describe "Check paths" do
before(:each) do
visit app.send("#{term.downcase}_path")
end
it "should have content #{term}" do
expect(page).to have_content(term)
end
end
end
The following rspec test file
require 'spec_helper'
describe EvaluationsController do
render_views
before(:each) do
association_attr
end
describe "'eval_selektor'" do
before(:each) do
#eval_selektor = get :eval_selektor, student_group_id: #student_group
end
it "should be successful" do
#eval_selektor
response.should be_success
end
...
end
...
end
is throwing the following error:
1) EvaluationsController 'eval_selektor' should be successful
Failure/Error: #eval_selektor = get :eval_selektor, student_group_id: #student_group
NoMethodError:
undefined method `student_groups' for nil:NilClass
# ./app/controllers/application_controller.rb:7:in `get_student_group'
# ./spec/controllers/evaluations_controller_spec.rb:14:in `block (3 levels) in <top (required)>'
from this method in the application_controller:
def get_student_group
#user = current_user
#student_group = #user.student_groups.find(params[:student_group_id])
end
At first I thought maybe rspec just wasn't getting passed the method from application_controller, but that's not the case as it can see it in the error. The code works in the browser, and if I put <%= #user %> in the view, it shows the correct user instance. Any ideas why rspec can't read #user?
apneadiving got me started in the right direction, and between this post and this one I got to the correct code:
ApplicationController.any_instance.stub(:current_user).and_return(#user)
I have a simple view and a helper that defines the title. Everything works fine if I pull up the view in the browser, but the rspec tests fail. Here's what I have:
Here are my tests:
describe PagesController do
render_views
before(:each) do
#base_title = "RoR Sample App"
end
describe "GET 'home'" do
it "should be successful" do
get 'home'
response.should be_success
end
it "should have the right title" do
get 'home'
response.should have_selector("title",
:content => #base_title + " | Home")
end
end
end
The Pages Controller:
class PagesController < ApplicationController
def home
#title = "Home"
end
def contact
#title = "Contact"
end
def about
#title = "About"
end
def help
#title = "Help"
end
end
The helper:
module ApplicationHelper
# Return a title on a per-page basis.
def title
base_title = "RoR22 Sample App"
if #title.nil?
base_title
else
"#{base_title} | #{#title}"
end
end
end
And the view:
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
</head>
<body>
<%= yield %>
</body>
</html>
It all renders properly in the browser, but the tests fail when it gets to <%= title %> line.
1) PagesController GET 'home' should be successful
Failure/Error: get 'home'
ActionView::Template::Error:
undefined local variable or method `title' for #<#<Class:0xabf0b1c>:0xabeec18>
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___418990135_90147100_123793781'
# ./spec/controllers/pages_controller_spec.rb:12:in `block (3 levels) in <top (required)>'
Any idea what I'm doing wrong?
This is a known issue with spork. You can use this workaround in your prefork block:
Spork.trap_method(Rails::Application, :reload_routes!)
Spork.trap_method(Rails::Application::RoutesReloader, :reload!)
That's your application.html.erb. It can't see that method in the PagesHelper, you need to put that method in your ApplicationHelper. Only the views rendered by your PagesController can see PagesHelper. And it's a good idea to explicitly add return in your helpers.
It ends up the problem is that I was using the Spork gem to speed up my testing and for whatever reason spork wasn't registering that the ApplicationHelper had changed.
The solution was simply to reboot the spork gem and rerun the tests.