The view is correctly displayed in the browser, but the tests do not work according to the helper method?

I have a simple view and helper that defines the title. Everything works fine if I pull the view in the browser, but the rspec tests fail. Here is 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 

Page 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 

Assistant:

 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> 

All of this displays correctly in the browser, but tests fail when they fall into the string <%= title %> .

  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?

+4
source share
3 answers

This is a known issue with spork. You can use this workaround in your pre-sale block:

 Spork.trap_method(Rails::Application, :reload_routes!) Spork.trap_method(Rails::Application::RoutesReloader, :reload!) 
+2
source

What is your application.html.erb . He cannot see this method in PagesHelper , you need to put this method in your ApplicationHelper . Only those displayed by your PagesController can see PagesHelper . And it is a good idea to explicitly add return to your helpers.

+1
source

As a result, the problem is that I used the Spork stone to speed up my testing and for some reason, Spork did not register ApplicationHelper changes.

The solution was to simply reload the Spork and repeat the tests.

0
source

All Articles