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.
Related
Context
I'm running into a very weird test failure that I can't explain based on my code.
When I run the spec test provided below, it will display the following error:
Failures:
1) GroupsController GET 'index' returns http success
Failure/Error: get 'index'
ActionController::UrlGenerationError:
No route matches {:action=>"index", :controller=>"groups"}
# ./spec/controllers/groups_controller_spec.rb:14:in `block (3 levels) in '
Test case
The test case for the controller and routes set in RSpec looks like this:
describe GroupsController do
before :each do
#group = FactoryGirl.create(:group)
#user = FactoryGirl.create(:user)
sign_in #user
end
describe "GET 'index'" do
it "returns http success" do
get 'index'
response.should be_success
end
end
end
Controller under test
I've written a very basic skeleton for my controller based on the test.
Currently it doesn't do a whole lot of stuff.
class GroupsController < ApplicationController
before_filter :authenticate_user!
def index
#groups = current_user.groups
end
end
Routes configured to reach the controller
The routes.rb file looks like this:
NerdCooking::Application.routes.draw do
resources :groups
devise_for :users
root :to => "home#welcome"
end
Routes
groups GET /groups(.:format) groups#index
POST /groups(.:format) groups#create
new_group GET /groups/new(.:format) groups#new
edit_group GET /groups/:id/edit(.:format) groups#edit
group GET /groups/:id(.:format) groups#show
PATCH /groups/:id(.:format) groups#update
PUT /groups/:id(.:format) groups#update
DELETE /groups/:id(.:format) groups#destroy
Question
I have tried changing the route to get "groups" => "groups#index" instead of the resources route and that works, but it's not something I want since I want to use this as a RESTful service as well.
What am I doing wrong here?
Update: Added the routes related to groups.
Okay, apparently my Guard and Spork were acting mean.
Once I restarted Guard/Spork and it all worked as expected. Looking back at the code and configuration there was no reason why stuff was going wrong.
So if anyone else is experiencing this behavior and their config and code check out. Restart!
I have a location resource and access via:
http://localhost:3000/locations/37/edit
In my spec, I have:
it "should allows us to edit" do
#u=User.find_by_email('jon#domain.com')
session[:user_id]=#u.id
get edit_location_path, {:id => '37'}
but get the following error:
Failures:
1) LocationsController should allows us to edit
Failure/Error: get edit_location_path, :id => '37'
ActionController::RoutingError:
No route matches {:action=>"edit", :controller=>"locations"}
# ./spec/controllers/locations_controller_spec.rb:12:in `block (2 levels) in <top (required)>'
How would I specify the link to this resource?
thx
Assuming it's a controller spec describing LocationsController, you can access it with get :edit, :id => 37.
Just do get edit_location_path(37) ??
I'm trying to write a very basic functional test for one of my controllers, but the problem is that it's not recognising any of my routes. They all work in the app via HTTP and can be seen in rake routes.
In fact I even added
puts ActionController::Routing::Routes.inspect
and it put out all my routes before the error.
Here's the test:
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
test "should get signup" do
get :new
assert_response :success
assert_not_nil assigns(:users)
end
end
The error:
1) Error:
test_should_get_signup(UsersControllerTest):
ActionController::RoutingError: No route matches {:controller=>"users", :action=>"new"}
/test/functional/users_controller_test.rb:5:in `test_should_get_signup'
1 tests, 0 assertions, 0 failures, 1 errors
rake aborted!
Command failed with status (1): [/Users/projectzebra/.rvm/rubies/ruby-1.8.7...]
(See full trace by running task with --trace)
Rake routes:
POST /users(.:format) {:controller=>"users", :action=>"create"}
new_user_en_gb GET /users/new(.:format) {:controller=>"users", :action=>"new"}
Try adding a functional test for the route itself, see if that passes, and go from there.
Something like:
test "should route to new user" do
assert_routing '/users/new', { :controller => "users", :action => "new" }
end
It was a problem in my "journey" gem. They made routes more stricter in journey 1.0.4 which only show up on "test" environment. It is good for "developement" and "production".
** Ensure you are using exactly the same parameters as declared in routes **
Either add:
get :index, :locale => "en"
or in your Gemfile update:
gem 'journey', '1.0.3'
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 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.