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
Related
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'
I am working on Michael Hartl's tutorial at railstutorial.org. I am having Difficulty In chapter 5 with getting the routing to work.
If I start with a routes file
routes.rb
Rails.application.routes.draw do
root 'static_pages#home'
get 'static_pages/help'
get 'static_pages/about'
get 'static_pages/contact'
for each of these there is a test like
static_pages_controller_test.rb
test "should get home" do
get :home
assert_response :success
assert_select "title", "Ruby on Rails Tutorial Sample App"
end
this syntax works and all the tests pass but later he wants to change the syntax using the *_path convention.
so now the tests look like
class StaticPagesControllerTest < ActionController::TestCase
test "should get home" do
get root_path
.
.
end
test "should get help" do
get help_path
.
.
end
and I updated the routes to
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
but now all the tests fail with the messages
ERROR["test_should_get_home", StaticPagesControllerTest, 2016-06-30 05:02:41 -0700]
test_should_get_home#StaticPagesControllerTest (1467288161.43s)
ActionController::UrlGenerationError: ActionController::UrlGenerationError:
No route matches {:action=>"/", :controller=>"static_pages"}
ERROR["test_should_get_help", StaticPagesControllerTest, 2016-06-30 05:02:41 -0700]
test_should_get_help#StaticPagesControllerTest (1467288161.43s)
ActionController::UrlGenerationError: ActionController::UrlGenerationError:
No route matches {:action=>"/help", :controller=>"static_pages"}
my controller looks something like this
class StaticPagesController < ApplicationController
def home
end
def help
end
.
.
end
if I run rake routes I get
Prefix Verb URI Pattern Controller#Action
root GET / static_pages#home
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
what am I doing wrong?
You need to rewrite these routes so that it can create dynamic route helpers for you as per your test. Write it like,
get 'static_pages/help' , as: :help
get 'static_pages/about' , as: :about
get 'static_pages/contact' , as: :contact
Read 3.6 Naming Routes.
As per your current route, those *_path will be like static_pages_about, static_pages_help etc. I am not sure how did you get the rake routes output like you have shown without using as option.
Yes Author has updated rails tutorials with 5.0.0 last week. It is suggested you update it too which will make further journey more pleasant and error free in addition to that, you will get more new things to learn in 5.0.0
update your tests/controllers/static_pages_controller_test.rb
require 'test_helper'
class StaticPagesControllerTest < ActionDispatch::IntegrationTest
test "should get home" do
get root_path
assert_response :success
assert_select "title", "Ruby on Rails Tutorial Sample App"
end
test "should get help" do
get help_path
assert_response :success
assert_select "title", "Help | Ruby on Rails Tutorial Sample App"
end
test "should get about" do
get about_path
assert_response :success
assert_select "title", "About | Ruby on Rails Tutorial Sample App"
end
test "should get contact" do
get contact_path
assert_response :success
assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
end
end
I am sure once you update your tests/controllers/static_pages_controller_test.rb you will see green test $ rails test
I'm not so sure, but what happens if you do this:
root 'static_pages#home'
get 'help', to: 'static_pages#help'
get 'about', to: 'static_pages#about'
get 'contact', to: 'static_pages#contact'
I'm trying to figure out how to test (minitest) a route that has locale and is within a scope:
scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
get 'static_pages/about'
resources :salas
end
This doesn't work:
require 'test_helper'
class StaticPagesControllerTest < ActionController::TestCase
test "should get about" do
get :about
assert_response :success
end
end
and will produce the following output:
# Running:
E
Finished in 0.301302s, 3.3189 runs/s, 0.0000 assertions/s.
1) Error:
StaticPagesControllerTest#test_should_get_about:
ActionController::UrlGenerationError: No route matches {:action=>"about", :controller=>"static_pages"}
test/controllers/static_pages_controller_test.rb:5:in `block in <class:StaticPagesControllerTest>'
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
rake routes:
Prefix Verb URI Pattern
Controller#Action
static_pages_about GET /:locale/static_pages/about(.:format) static_pages#about {:locale=>/en|es/}
salas GET /:locale/salas(.:format) salas#index {:locale=>/en|es/}
POST /:locale/salas(.:format) salas#create {:locale=>/en|es/}
new_sala GET /:locale/salas/new(.:format) salas#new {:locale=>/en|es/}
edit_sala GET /:locale/salas/:id/edit(.:format) salas#edit {:locale=>/en|es/}
sala GET /:locale/salas/:id(.:format) salas#show {:locale=>/en|es/}
PATCH /:locale/salas/:id(.:format) salas#update {:locale=>/en|es/}
PUT /:locale/salas/:id(.:format) salas#update {:locale=>/en|es/}
DELETE /:locale/salas/:id(.:format) salas#destroy {:locale=>/en|es/}
GET /*path(.:format) redirect(301, /en/%{path})
root GET / salas#index
I think that maybe I need to pass the locale to the test but I'not sure how.
Thanks !
A little below this heading: Function Tests for your Controllers in the Rails Guide: A Guide to Testing Rails shows the get method's convention. To pass params to the method, include it as the second parameter to the call.
get :about, locale: 'en'
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 ??
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'