I got an error while testing code from RoR tutorial, pls help with these issue, listing is below. May be wrong version of spec in Gemfile.
Failures:
1) PagesController GET 'home' should have the right title
Failure/Error: response.should have_selector("title", :content => "Home")
NoMethodError:
undefined method `has_selector?' for #<ActionController::TestResponse:0x007fb3d4d2d108>
# ./spec/controllers/pages_controller_spec.rb:14:in `block (3 levels) in <top (required)>'
2) PagesController GET 'contact' should have the right title
Failure/Error: response.should have_selector("title", :content => "Contact")
NoMethodError:
undefined method `has_selector?' for #<ActionController::TestResponse:0x007fb3d280b370>
# ./spec/controllers/pages_controller_spec.rb:26:in `block (3 levels) in <top (required)>'
3) PagesController GET 'about' should have the right title
Failure/Error: response.should have_selector("title", :content => "About")
NoMethodError:
undefined method `has_selector?' for #<ActionController::TestResponse:0x007fb3d2b96e90>
# ./spec/controllers/pages_controller_spec.rb:38:in `block (3 levels) in <top (required)>'
4) PagesController GET 'help' should have the right title
Failure/Error: response.should have_selector("title", :content => "Help")
NoMethodError:
undefined method `has_selector?' for #<ActionController::TestResponse:0x007fb3d28a19d8>
# ./spec/controllers/pages_controller_spec.rb:50:in `block (3 levels) in <top (required)>'
Finished in 0.1005 seconds
8 examples, 4 failures
Failed examples:
rspec ./spec/controllers/pages_controller_spec.rb:12 # PagesController GET 'home' should have the right title
rspec ./spec/controllers/pages_controller_spec.rb:24 # PagesController GET 'contact' should have the right title
rspec ./spec/controllers/pages_controller_spec.rb:36 # PagesController GET 'about' should have the right title
rspec ./spec/controllers/pages_controller_spec.rb:48 # PagesController GET 'help' should have the right title
Gemfile:
source 'https://rubygems.org'
gem 'rails', '3.2.3'
gem 'sqlite3'
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
group :test, :development do
gem "rspec-rails", "~> 2.10.1"
end
pages_controller_spec.rb listing:
require 'spec_helper'
describe PagesController do
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 => "Home")
end
end
describe "GET 'contact'" do
it "should be successful" do
get 'contact'
response.should be_success
end
it "should have the right title" do
get 'contact'
response.should have_selector("title", :content => "Contact")
end
end
describe "GET 'about'" do
it "should be successful" do
get 'about'
response.should be_success
end
it "should have the right title" do
get 'about'
response.should have_selector("title", :content => "About")
end
end
describe "GET 'help'" do
it "should be successful" do
get 'help'
response.should be_success
end
it "should have the right title" do
get 'help'
response.should have_selector("title", :content => "Help")
end
end
end
I do not understang what is wrong.
If you don't want to use webrat, you can use
response.body.should include 'Contact</h1>'
Rather than
response.should have_selector("title", :content => "Contact")
Which is a bit less clean, I agree.
Re-edit your GEMFILE and type
gem 'webrat'
and then
type
bundle install
Related
I am using Rails 4.0.8. When I ran bundle exec rspec spec/, here's the error I got from the tutorial (http://www.railstutorial.org/book/filling_in_the_layout):
Pending: StaticPagesHelper add some examples to (or delete)
/Users/Desktop/sample_app/spec/helpers/static_pages_helper_spec.rb
# No reason given
# ./spec/helpers/static_pages_helper_spec.rb:14
Failures:
1) StaticPagesController GET '...' returns http success
Failure/Error: get '...'
ActionController::UrlGenerationError:
No route matches {:action=>"...", :controller=>"static_pages"}
# ./spec/controllers/static_pages_controller_spec.rb:7:in `block (3 levels) in '
Finished in 0.20849 seconds 19 examples, 1 failure, 1 pending
Failed examples:
rspec ./spec/controllers/static_pages_controller_spec.rb:6 #
StaticPagesController GET '...' returns http success
Here is my route.rb file:
SampleApp::Application.routes.draw do
resources :users
root to: 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
end
Here is my static_pages_helper_spec.rb file:
require 'spec_helper'
# Specs in this file have access to a helper object that includes
# the StaticPagesHelper. For example:
#
# describe StaticPagesHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# helper.concat_strings("this","that").should == "this that"
# end
# end
# end describe StaticPagesHelper do pending "add some examples to (or delete) #{__FILE__}" end
Here is my static_pages_controller_spec.rb file:
require 'spec_helper'
describe StaticPagesController do
describe "GET '...'" do
it "returns http success" do
get '...'
response.should be_success
end
end
end
app/controllers/static_pages_controller.rb
class StaticPagesController < ApplicationController
def home
end
def help
end
def about
end
def contact
end
end
I wonder whether I'm getting the error because my version is not compatible with his tutorial. Should I be looking at this tutorial instead http://rails-3-2.railstutorial.org/book/filling_in_the_layout#sec-rails_routes?
Your static_pages_controller_spec has
describe "GET '...'" do
it "returns http success" do
get '...'
response.should be_success
end
end
The "..." is nonsense in this context and the tutorial probably included it as a "specimen" entry.
Comment out the above lines, or replace "..." with a real method in your static_pages_controller.rb. You probably have an index method so you could do...
describe "GET index" do
it "returns http success" do
get :index
response.should be_success
end
end
Cheers
Ok so I want to create a test for checking that all pages have a certain title. However I thought it would be nice if I could include the page titles in an array so that I wouldn't have to duplicate the block for each page. And it would allow me to test additional pages by just modifying the pages array.
The issue I am having is that the page variable is not being interpolated in the test. So is this a syntax error or does Rspec not allow interpolation within the it should do... block?
describe "LayoutLinks" do
page = ["home", "contact", "about", "help"]
i = page.count
x = 0
while x < i
it "should have a #{page[x]} page" do
get "#{page[x]}"
response.should have_selector("title", :content => "#{page[x]}")
end
x += 1
end
end
Test shows the following failures:
1) PagesController LayoutLinks should have a help page
Failure/Error: get "#{page[x]}"
ActionController::RoutingError:
No route matches {:controller=>"pages", :action=>""}
# ./spec/controllers/layout_links_spec.rb:14:in `block (3 levels) in <top (required)>'
2) PagesController LayoutLinks should have a contact page
Failure/Error: get "#{page[x]}"
ActionController::RoutingError:
No route matches {:controller=>"pages", :action=>""}
# ./spec/controllers/layout_links_spec.rb:14:in `block (3 levels) in <top (required)>'
3) PagesController LayoutLinks should have a about page
Failure/Error: get "#{page[x]}"
ActionController::RoutingError:
No route matches {:controller=>"pages", :action=>""}
# ./spec/controllers/layout_links_spec.rb:14:in `block (3 levels) in <top (required)>'
4) PagesController LayoutLinks should have a home page
Failure/Error: get "#{page[x]}"
ActionController::RoutingError:
No route matches {:controller=>"pages", :action=>""}
Failure error here is obvious. It shouldn't say get "#{page[x]}" but rather it should be get home and get about, etc...
How do I remedy? Thanks for the help. Much appreciated :)
Try the following:
describe "LayoutLinks" do
%w(home contact about help).each do |page|
it "should have a #{page} page" do
get page
response.should have_selector("title", content: page)
end
end
end
%w creates an array of strings (spaces become commas, so you get ["home", "contact", etc])
I have this resource in my config/routes.rb
resources :users, :path => "u" do
resources :boards, :controller => 'users/boards', :path => "collections"
end
In my spec/routing/boards_routing_spec.rb i have:
require "spec_helper"
describe Users::BoardsController do
describe "routing" do
it "routes to #index" do
get("/u/:user_id/collections").should route_to("users/boards#index")
end
end
end
I get the next error:
Failures:
1) Users::BoardsController routing routes to #index
Failure/Error: get("/u/:user_id/collections").should route_to("users/boards#index")
The recognized options <{"action"=>"index", "controller"=>"users/boards", "user_id"=>":user_id"}> did not match <{"controller"=>"users/boards", "action"=>"index"}>, difference: <{"user_id"=>":user_id"}>.
<{"controller"=>"users/boards", "action"=>"index"}> expected but was
<{"action"=>"index", "controller"=>"users/boards", "user_id"=>":user_id"}>.
# ./spec/routing/boards_routing_spec.rb:7:in `block (3 levels) in <top (required)>'
I am using Devise 2.0 for user.
How can I fix this error?
The error was fixed:
require "spec_helper"
describe Users::BoardsController do
describe "routing" do
it "routes to #index" do
get("/u/hyperrjas/collections").should route_to("users/boards#index", :user_id => "hyperrjas")
end
end
I created a controller called PolicyController and nested its route like this:
scope "/your"
resources :shops do
resources :policies
end
end
Now when I'm trying to test this controller I keep getting this error:
1) PoliciesController POST 'create' should be successful
Failure/Error: post 'create'
ActionController::RoutingError:
No route matches {:controller=>"policies", :action=>"create"}
# ./spec/controllers/policies_controller_spec.rb:7:in `block (3 levels) in <top (required)>'
Not sure how to set it right. Would appreciate the help.
Edit: Forgot my specs:
describe PoliciesController do
describe "POST 'create'" do
it "should be successful" do
post 'create'
response.should be_success
end
end
Do you think this will work?
post :create, :shop_id => 1
Definitely want to create a new shop in a before block.
I'm trying to follow THE magnificient Rails3 tutorial at http://railstutorial.org but ran into a problem in chapter 5.
How ironic, but i'm getting stack overflow running these LayoutLinks (http://railstutorial.org/chapters/filling-in-the-layout#sec:integration_tests) specs.
This is the output:
C:\sample_app>rspec spec\requests\layout_links_spec.rb
FFFFFF
Failures:
1) LayoutLinks should have a Home page at '/'
Failure/Error: response.should have_selector('title', :content => "Home")
stack level too deep
# ./spec/requests/layout_links_spec.rb:7
2) LayoutLinks should have a Contact page at '/contact'
Failure/Error: response.should have_selector('title', :content => "Contact")
stack level too deep
# ./spec/requests/layout_links_spec.rb:12
3) LayoutLinks should have an About page at '/about'
Failure/Error: response.should have_selector('title', :content => "About")
stack level too deep
# ./spec/requests/layout_links_spec.rb:17
4) LayoutLinks should have a Help page at '/help'
Failure/Error: response.should have_selector('title', :content => "Help")
stack level too deep
# ./spec/requests/layout_links_spec.rb:22
5) LayoutLinks should have a Help page at '/help'
Failure/Error: response.should have_selector('title', :content => "Help")
stack level too deep
# ./spec/requests/layout_links_spec.rb:27
6) LayoutLinks should have a signup page at '/signup'
Failure/Error: response.should have_selector('title', :content => "Sign up")
stack level too deep
# ./spec/requests/layout_links_spec.rb:32
Finished in 206.38 seconds
6 examples, 6 failures
The code and specs ought to be the same as on that tutorial (i copy-pasted).
Any ideas what might be happening here?
I tried to run specs with -b to get more stacktrace but it didn't work for some reason.
I'm using Ruby 1.8.7, Rails 3.0.1 and RSpec 2.1.0 on Windows 7.
Seems like you are running into this Webrat issue: https://github.com/rspec/rspec-rails/issues#issue/140.
Try downgrading to Webrat 0.7.1 in your gemfile.