My time is disappointed with Rspec, working on a Ruby on Rails Learn in Example 5.2 tutorial.
I am currently trying to verify my routing with rspec, and the tests always return as a failure, although I correctly followed the steps in the tutorial as far as I know, and confirmed that routing works by manually checking and testing the routing itself.
For example, layout_links_spec.rb looks like this:
require 'spec_helper' describe "LayoutLinks" do it "should have a Home page at '/'" do get '/' response.should have_selector('title', :content => "Home") end it "should have a Contact page at '/contact'" do get '/contact' response.should have_selector('title', :content => "Contact") end it "should have an About page at '/about'" do get '/about' response.should have_selector('title', :content => "About") end it "should have a Help Page at '/help'" do get '/help' reponse.should have_selector('title', :content => "Help") end end
And my routes look like this:
SampleApp::Application.routes.draw do match '/contact', :to => 'pages#contact' match '/about', :to => 'pages#about' match '/help', :to => 'pages#help' get "pages/home" get "pages/contact" get "pages/about" get "pages/help" root :to => 'pages#home' end
Rspec returns this failure:
5) LayoutLinks should have a Home page at '/' Failure/Error: get '/' ActionController::RoutingError: No route matches [GET] "/" # ./spec/requests/layout_links_spec.rb:6:in `block (2 levels) in <top (required)>' 6) LayoutLinks should have a Contact page at '/contact' Failure/Error: get '/contact' ActionController::RoutingError: No route matches [GET] "/contact"
I get a similar error with my tests page_controller_spec.rb
describe "GET 'home'" do it "should be successful" do get 'home' response.should be_success end
Gets this rspec error:
1) PagesController GET 'home' should be successful Failure/Error: get 'home' ActionView::Template::Error: undefined local variable or method `root_path' for #<#<Class:0x00000103e8b210>:0x00000103e87de0> # ./app/views/layouts/_header.html.erb:3:in `_app_views_layouts__header_html_erb__1560745193027372362_2179584160' # ./app/views/layouts/application.html.erb:10:in `_app_views_layouts_application_html_erb__2627526215404316040_2176993080'
I checked my code with a tutorial and it looks right. If I type url localhost: 3000 / home or / about etc., I get to the correct page, which indicates that it should be properly routed. I could just continue the tutorial, but I would like to learn how to do TDD and use rspec in practice, but I feel that I can not rely on rspec for this ...