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'
Related
I am following a tutorial of Rails and I have a few pages that I am adding some tests for.
I am trying to use help_path instead of :help in my pages_controller_test :
test "should get help" do
get help_path
assert_response :success
assert_select "title", "Help | #{#base_title}"
end
I added this line in my routes.rb file :
get '/help', to: 'pages#help'
But I get this error :
1) Error:
PagesControllerTest#test_should_get_help:
ActionController::UrlGenerationError: No route matches {:action=>"/help", :controller=>"pages"}
test/controllers/pages_controller_test.rb:62:in `block in '
I have tried a few solutions but none of them solved my issue.
I've also tried using this line instead :
match '/home' => 'main_pages#home', :as => :home
But it didn't work either.
My rails version is : 4.2.4
My Ruby version is : ruby 2.1.9p490 (2016-03-30 revision 54437) [x86_64-linux-gnu]
Output of $rake routes :
Prefix Verb URI Pattern Controller#Action
root GET / pages#home
help GET /help(.:format) pages#help
courses GET /courses(.:format) pages#courses
programs GET /programs(.:format) pages#programs
schools GET /schools(.:format) pages#schools
dashboard GET /dashboard(.:format) pages#dashboard
profile GET /profile(.:format) pages#profile
account GET /account(.:format) pages#account
signout GET /signout(.:format) pages#signout
EDIT :
I can use help_path .. etc in my html code without any issue, but in the test it gives that error.
Thank you :)
I've ran your tests using the repo, Rails 4.2.4, Minitest 5.10.2 and the only one test that doesn't pass is the one using get help_path. I put only the 3 tests in order to shorten the post:
PagesControllerTest#test_should_get_help_using_"get_:help" = 0.39 s = .
PagesControllerTest#test_should_get_help_using_"get_help_path" = 0.00 s = E
PagesControllerTest#test_should_get_help_using_"get_'help'" = 0.01 s = .
Finished in 0.405330s, 7.4014 runs/s, 9.8685 assertions/s.
1) Error:
PagesControllerTest#test_should_get_help_using_"get_help_path":
ActionController::UrlGenerationError: No route matches {:action=>"/help", :controller=>"pages"}
test/controllers/pages_controller_test.rb:70:in `block in <class:PagesControllerTest>'
3 runs, 4 assertions, 0 failures, 1 errors, 0 skips
What I did:
$ rm -rf Gemfile.lock (because of a json -v 1.8.1 gem error)
$ bundle
$ rake db:migrate
$ rake db:migrate RAILS_ENV=test
$ rake test test/controllers/pages_controller_test.rb -v
What you can use to make the test work with help_path as the route to test specifying the controller and action is to use:
assert_routing asserts that the routing of the given path was handled correctly and
that the parsed options (given in the expected_options hash) match
path. Basically, it asserts that Rails recognizes the route given by
expected_options.
Or:
assert_recognizes asserts that path and options match both ways; in other words, it
verifies that path generates options and then that options generates
path. This essentially combines assert_recognizes and assert_generates
into one step.
Like:
test "should get help using assert_recognizes and help_path" do
assert_recognizes({controller: 'pages', action: 'help'}, help_path)
assert_response :success
end
test "should get help using assert_routing and help_path" do
assert_routing help_path, controller: 'pages', action: 'help'
assert_response :success
end
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'
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!
Routes that work fine in my application fail on any get/put call in rspec testing with 'No route matches'. What am I doing wrong here?
Here's a simple example, from contracts_controller_spec.rb:
it 'should redirect to edit on show' do
get :show
response.should be_success
response.should render_template(:edit)
end
The above fails with the following:
ContractsController api calls should redirect to edit on show
Failure/Error: get :show
ActionController::RoutingError:
No route matches {:controller=>"contracts", :action=>"show"}
the show method in contracts_controller.rb:
def show
Rails.logger.debug("getting contract info....")
get_contract_info
Rails.logger.debug("...got contract info.")
render :action => :edit
end
routes.rb content:
resource :contract, :only=>[:show, :edit, :update], :protocol =>ROUTES_PROTOCOL do
member do
get :print
end
end
rake routes output:
print_contract GET /contract/print(.:format) contracts#print {:protocol=>"http"}
edit_contract GET /contract/edit(.:format) contracts#edit {:protocol=>"http"}
contract GET /contract(.:format) contracts#show {:protocol=>"http"}
PUT /contract(.:format) contracts#update {:protocol=>"http"}
using rspec-rails 2.14.0
This app was recently upgraded from Rails 2.3 to 3.2, which has otherwise been successful
note the non-standard show/edit routes: no id is required, and forcing an id still results in No route matches {:controller=>"contracts", :id=>"1", :action=>"show"}
:protocol is bit weird, try to remove ??
In config/routes.rb:
match 'app_config/:version' => "application#appconfig"
test/functional/application_controller_test.rb:
require 'test_helper'
class ApplicationControllerTest < ActionController::TestCase
test "app config" do
get "/app_config/v2"
assert_response :success
end
end
rake test:
1) Error:
test_app_config(ApplicationControllerTest):
ActionController::RoutingError: No route matches {:controller=>"application", :action=>"/app_config/v2"}
but $ curl localhost:3000/app_config/v2 works, and returns the response I expect, and rake routes shows the route as expected. Any idea what's going on, or how to investigate further? This is basically the only code in the project.
The get method does not take a route. It takes an action name and a parameter's hash.
If you want to test the action, your tests should look like this:
test "for success" do
get :appconfig, :version => "v2"
assert_response :success
end
If you want to test routing there is a tutorial here in the documentation.
test "should route to appconfig" do
assert_routing '/app_config/v2', { :controller => "application", :action => "appconfig", :version => "v2" }
end