No route matches on Railstutorial.org - ruby-on-rails

I generated home and contact page thru:
rails generate Pages home contact
did tests to verify and all was okay, now I wanted to add the page "about". I created the about.html.erb through copying the contact.html.erb and pasting then renaming it to about.html.erb. I then changed the content to "Pages#about" instead of "Pages#contact"
I changed route.rb to:
SampleApp::Application.routes.draw do
get "pages/home"
get "pages/contact"
get "pages/about"
then pages_controller.rb to:
def home
end
def contact
end
def about
end
Finally added this to pages_controller_spec.rb:
describe "GET 'about'" do
it "should be successful" do
get 'about'
response.should be_success
end
end
on my autotest this was the error:
Failures:
1) PagesController GET 'about' should be successful
Failure/Error: get 'about'
No route matches {:controller=>"pages", :action=>"about"}
# ./spec/controllers/pages_controller_spec.rb:22:in `block (3 levels) in <top (required)>'
What did I do wrong?
Should I have generated the about page through:
rails generate Pages about
to generate the about page? instead of copy-paste?

Had the same problem.
In my case the problem was that 'spork' needed a restart

This is because spork doesn't reload your routes. Put this in your spec_helper.rb to force spork to reload routes "each_run" (credit: http://jinpu.wordpress.com/2011/03/13/reload-routes-with-spork-each-run/)
Spork.each_run do
# This code will be run each time you run your specs.
require File.expand_path("../../config/routes", __FILE__)
end

Samesies: restart spork
It was only after I quit in frustration and came back an hour later for another look that it worked.

Related

Rspec not finding routes that exists

I'm trying to write a controller test and Rspec isn't finding routes that I know exist and work fine on a development server.
In my routes I have a catch-all route that should redeirect to a generic controller if someone goes to a route that isn't predefined.
routes.rb
namespace :tools do
match '*unmatchedpath' => "generic#show", :via => :get
end
generic_controller.rb
def show
# do stuff
end
generic_controller_spec.rb
require 'spec_helper'
describe Tools::GenericController do
describe 'GET show' do
it 'does stuff' do
get :show
end
end
Here is the error I get from rspec when I run the test above
1) Tools::GenericController GET show does stuff
Failure/Error: get :show
ActionController::RoutingError:
No route matches {:controller=>"tools/generic", :action=>"show"}
All routes work as expected on my development server so I'm not sure why Rspec isn't finding the route.
Try:
get '*unmatchedpath' => 'tools/generic#show'

guard error ActionController::UrlGenerationError:

I am starting to write tests for my rails application, and plan to write them prior to building the actual pages, but I am having trouble. I have searched stack overflow and google and have tried several things. The route is specified in my routes.rb file and looks like:
resources :welcome
I have the following written into my test code.
require 'rails_helper'
require 'spec_helper'
RSpec.describe "Welcomes", type: :request do
it "checks the welcome page." do
visit welcome_path
end
end
I get the following Error from guard:
Failures:
1) Welcomes checks the welcome page.
Failure/Error: visit welcome_path
ActionController::UrlGenerationError:
No route matches {:action=>"show", :controller=>"welcome"} missing required keys: [:id]
# ./spec/requests/welcomes_spec.rb:6:in `block (2 levels) in <top (required)>'
Finished in 0.00347 seconds (files took 1.61 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/requests/welcomes_spec.rb:5 # Welcomes checks the welcome page.
Change visit welcome_path to get '/welcome'
The index of a Rails resources route set is at welcomes_path. welcome_path would refer to a specific Welcome and requires an :id parameter.
If the welcome page is just a landing page and not actually a resource, you might just want config/routes.rb to contain:
get 'welcome', to: 'welcome#index`
The first welcome is the path (host.com/welcome), and the to: argument is controller#action.
The Rails routing docs are great. Give them a good read through if this didn't make sense.

Rspec no route matches

I'm getting the following error with rspec:
1) LandingController landing#index returns http success
Failure/Error: get :index
ActionController::RoutingError:
No route matches {:controller=>"landing"}
# ./spec/controllers/landing_controller_spec.rb:7:in `block (3 levels) in <top (required)>'
This is the test
require 'spec_helper'
describe LandingController do
describe "landing#index" do
it "returns http success" do
get :index
response.should be_success
end
end
end
I mounted it as root :to => 'landing#index'. All other tests are passing, only this one is failing, can someone help me to understand why?
For completeness this is the output from rake routes
root / landing#index
auth_google_oauth2_callback /auth/google_oauth2/callback(.:format) sessions#create
signout /signout(.:format) sessions#destroy
dashboard /dashboard(.:format) dashboard#index
If you are using Spork you may need to restart the server if you updated routes.
did you try to access the root page with get '/'? should work.

Why doesn't this Rails Rspec Test Work?

I create all the routes of my program manually and so do with my rspec tests of course. Generally, my routes and tests work fine, but i have a problem with the test for my characters controller. The route is :
scope :path => '/characters', :controller => :characters do
get '/' => :show, :as => 'user_character'
end
The /characters works fine when tested with my browser. Everything seems fine. But, the test :
require 'spec_helper'
require 'devise/test_helpers'
describe CharactersController do
login_user
describe "when it GETS 'show'" do
it "should render the template and be successful" do
get :show
response.should render_template(:show)
response.should be_success
end
end
end
Fails with the error :
1) CharactersController when it GETS 'show' should render the template and be successful
Failure/Error: get :show
ActionController::RoutingError:
No route matches {:controller=>"characters", :action=>"show"}
# ./spec/controllers/characters_controller_spec.rb:9
All my controllers have similar tests that work fine. Why does this not work ?
IMPORTANT EDIT :
Just saw that if i turn Spork off, the test passes ! Why is this happening ? Does Spork need to be restarted every time a new test is added ?
You have to restart spork when changing routes.
Or put this in your spec_helper.rb:
Spork.each_run do
ApplicationName::Application.reload_routes!
end
See also "Speedy Test Iterations for Rails 3 with Spork and Guard"

Rspec 2.3 on Rails 3.0.3 giving some controller access problems?

It seems a bunch of my Rspec tests now fail after moving my application to Rspec 2.3 and Rails 3.0.3
An example is here:
it "should not be able to access 'destroy'" do
delete :destroy
response.should redirect_to(signin_path)
flash[:error].should == "You must be signed in to view this page."
end
will give me the error:
1) FriendshipsController when not logged in: should not be able to access 'destroy'
Failure/Error: delete :destroy
No route matches {:controller=>"friendships", :action=>"destroy"}
# ./spec/controllers/friendships_controller_spec.rb:21:in `block (3 levels) in <top (required)>'
In my routes.rb file, I've mapped the resources for this controller...
resources :friendships
Same for
get :edit
get :show
put :update
Only one that seems to work is
post :create
But this I cannot confirm 100%.
Any thoughts? Thanks for your time!
UPDATE:
get :new
also works and my UserSessions controller (Authlogic) doesn't seem to suffer from this problem. Nothing I've done different in my UserSessions controller, model, or test that I can tell.
In the spec, try calling the method by:
delete :destroy, :id => "1"

Resources