I am currently learning Rails Tutorial. I have error messages after rails test, TDD. There are more messages, but first of all i want to debug these. Even though rails test is not working well, application itself is working without problem as of now.
I took a look this sample codes.
https://github.com/yasslab/sample_apps
Error messages
11) Error:
StaticPagesControllerTest#test_should_get_about:
NameError: undefined local variable or method `static_pages_about_url' for #<StaticPagesControllerTest:0x00000006f89228>
StaticPagesControllerTest#test_should_get_home:NameError: undefined local variable or method `static_pages_home_url' for #<StaticPagesControllerTest:0x00000006f964a0>
test/controllers/static_pages_controller_test.rb:7:in `block in <class:StaticPagesControllerTest>' 13) Error:
StaticPagesControllerTest#test_should_get_contact:NameError: undefined local variable or method `static_pages_contact_url' for #<StaticPagesControllerTest:0x0000000623d600>
test/controllers/static_pages_controller_test.rb:27:in `block in <class:StaticPagesControllerTest>' 14) Error:StaticPagesControllerTest#test_should_get_help:NameError: undefined local variable or method `static_pages_help_url' for #<StaticPagesControllerTest:0x00000006f9d6d8> test/controllers/static_pages_controller_test.rb:13:in `block in <class:StaticPagesControllerTest>'
static_pages_controller.rb
class StaticPagesController < ApplicationController
def home
end
def help
end
def about
end
def contact
end
end
static_pages_controller_test.rb
class StaticPagesControllerTest < ActionController::TestCase
test "should get home" do
get static_pages_home_url
assert_response :success
assert_select "title", "Ruby on Rails Tutorial Sample App"
end
test "should get help" do
get static_pages_help_url
assert_response :success
assert_select "title", "Help | Ruby on Rails Tutorial Sample App"
end
test "should get about" do
get static_pages_about_url
assert_response :success
assert_select "title", "About | Ruby on Rails Tutorial Sample App"
end
test "should get contact" do
get static_pages_contact_url
assert_response :success
assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
end
end
routes.rb
Rails.application.routes.draw do
get 'sessions/new'
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users
end
I want to make these errors into assertions.
Could you kindly help me?
Related
i'm a ROR newbie who is going through the tutorial by Micheal Hartl. I have follow every steps of the tutorial and i'm stuck with the error regarding the routes. I have gone through all my codes and tried all the solution from google. Long story short i'm trying to get the test pass.
Here is my routes.rb
Rails.application.routes.draw do
root 'static_pages#home'
get '/help', to: 'static_pages#help'
end`
Here is the error description
1)Error StaticPagesControllerTest#test_should_get_home:
ActionController::UrlGenerationError: No route matches {:action=>"/", :controller=>"static_pages"} test/controllers/static_pages_controller_test.rb:10: in block in class:StaticPagesControllerTest'
2) Error StaticPagesControllerTest#test_should_get_help:
ActionController::UrlGenerationError: No route matches {:action=>"/help", :controller=>"static_pages"}
test/controllers/static_pages_controller_test.rb:16:in `block in class:StaticPagesControllerTest'
Here is how my rake routes looks like
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
I have tried
root 'static_pages#home'
get '/static_pages/help', to: 'static_pages#help'
or
root 'static_pages#home'
match '/help', :to => 'static_pages#help', via: [:get]
yet i still cannot solve the problem. I have been staring at this code and error for days. Please help.
*Edit to include test
test "should get home" do
get root_path
assert_response :success
assert_select "title", "Home | #{#base_title}"
end
test "should get help" do
get help_path
assert_response :success
assert_select "title", "Help | #{#base_title}"
end
I've been following the Michael Hartl tutorial for learning rails and have been doing pretty well thus far, but I'm stumped on this issue which I've encountered repeatedly. I'm not skilled enough to know if there is a configuration problem in my environment or something else, but this makes NO sense to me.
In ANY of the controller tests I attempt to run, I can never get my helper URL methods to work. As an example:
test "should redirect home when not an admin" do
log_in_as(#other_user)
assert_no_difference 'User.count' do
delete user_path(#user)
end
assert redirect_to root_url
end
Generates the following error:
ERROR["test_should_redirect_home_when_not_an_admin", UsersControllerTest, 0.9899668790167198]
test_should_redirect_home_when_not_an_admin#UsersControllerTest (0.99s)
ActionController::UrlGenerationError: ActionController::UrlGenerationError: No route matches {:action=>"/users/762146111", :controller=>"users"}
test/controllers/users_controller_test.rb:60:in `block (2 levels) in <class:UsersControllerTest>'
test/controllers/users_controller_test.rb:59:in `block in <class:UsersControllerTest>'
Even simple tests like
test "should redirect index when not logged in" do
get users_path
assert_redirected_to login_url
end
Produce the same error:
ERROR["test_should_redirect_index_when_not_logged_in", UsersControllerTest, 1.5291320629185066]
test_should_redirect_index_when_not_logged_in#UsersControllerTest (1.53s)
ActionController::UrlGenerationError: ActionController::UrlGenerationError: No route matches {:action=>"/users", :controller=>"users"}
test/controllers/users_controller_test.rb:34:in `block in <class:UsersControllerTest>'
Everything I've googled about this issue hasn't helped, because for some reason I cannot determine the user_path method (or any other similar method) somehow thinks the action I'm trying to take in my controller is the path!
I've verified that my routes file is configured correctly.
Rails.application.routes.draw do
root 'static_pages#home'
get '/help' => 'static_pages#help'
get '/about' => 'static_pages#about'
get '/contact' => 'static_pages#contact'
get '/signup' => 'users#new'
post '/signup' => 'users#create'
get '/login' => 'sessions#new'
post '/login' => 'sessions#create'
delete '/logout' => 'sessions#destroy'
get 'sessions/new'
resources :users
end
I've checked that running "rails routes" has the correct routes. I've even checked in rails console that "app.user_path(1)" spits out a valid path.
At this point I'm just beyond stumped as to why these helper methods don't seem to be helping... I also don't know what they're actually called so my googling has been fairly fruitless.
To get around this issue in the tutorial, I've been using syntax like
patch :edit, { id: #user }, params: { user: { name: #user.name,
email: #user.email }}
Or
test "should redirect index when not logged in" do
get :index
assert_redirected_to login_url
end
Which seems to work.
Also here is one of my test files if that's helpful:
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
def setup
#user = users(:michael)
#other_user = users(:archer)
end
test "should get new" do
get :new
assert_response :success
end
test "should redirect edit when user not logged in" do
get :edit, params: { id: #user }
assert_not flash.empty?
assert_redirected_to login_url
end
test "should redirect update when user not logged in" do
patch :edit, { id: #user }, params: { user: { name: #user.name,
email: #user.email }}
assert_not flash.empty?
assert_redirected_to login_url
end
test "should redirect index when not logged in" do
# get users_path
get :index
assert_redirected_to login_url
end
test "should redirect destroy when not logged in" do
assert_no_difference 'User.count' do
delete user_path(#user)
end
assert redirect_to login_url
end
test "should redirect home when not an admin" do
log_in_as(#other_user)
assert_no_difference 'User.count' do
delete user_path(#user)
end
assert redirect_to root_url
end
end
Please let me know what other files I can post to be helpful.
I had the same issue and I fixed it by changing the file 'users_controller_test.rb' file replacing:
class UsersControllerTest < ActionController::TestCase
for
class UsersControllerTest < ActionDispatch::IntegrationTest
But first, in your case, check that you have in your Gemfile the line:
gem 'rails', '5.0.1'
as the tutorial shows.
I can also see that you have the following test:
test 'should get new' do
get :new
assert_response :success
end
When you make the correction you'll have to change the above test to:
test 'should get new' do
get new_user_path
assert_response :success
end
After that, run your tests and you should have them all passed.
Hope it helps
Doing Michael Hartl's Rails Tutorial, but stumped on Listing 5.28 (getting RED test instead of GREEN), changing the test to match the new routes.
ERRORS for all pages (/, /about, /contact, /help):
ActionController::UrlGenerationError: No route matches {:action=>"/*", :controller=>"static_pages"}
routes.rb
Rails.application.routes.draw do
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
end
tests/controllers/static_pages_controller_test.rb
require 'test_helper'
class StaticPagesControllerTest < ActionController::TestCase
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
static_pages_controller.rb
class StaticPagesController < ApplicationController
def home
end
def help
end
def about
end
def contact
end
end
Let me know if you need to see any other code! Have tried adding as: '*' after each get route, but to no avail.
Not sure if it is a ruby/rails version issue, but I am using Rails 4.2.2 and Ruby 2.3.0 on an IDE, but "rails test" (as Hartl instructs to use) won't work (kicks back "test Command not found"). Not sure if that's a hint to a bigger problem or unrelated. Thanks in advance!
EDIT: Links using these paths (like below) are rendering correctly, it is just failing the tests.
<%= link_to "Home", root_path %>
<%= link_to "Help", help_path %>
Your test/controllers/static_pages_controller_test.rb have problem. I would suggest to replace it with below contents.
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
Change the above file as I have suggested and run $ rails test you can see green test. I also suggest you to upgrade your app to rails 5.0.0 as Michael Hartl's have been upgraded his rails tutorials to rails 5.0.0 in future you have more things to learn and if you upgrade it, your journey of learning would be more error free and pleasant.
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'
So I am following Michael Hartl's Rails tutorial and I am on chapter 5 (https://www.railstutorial.org/book/filling_in_the_layout).
When I run the final test that he adds for the sign up link:
class UsersControllerTest < ActionDispatch::IntegrationTest
test "should get new" do
get signup_path
assert_response :success
end
end
I get this error when I run the test:
ERROR["test_should_get_new", UsersControllerTest, 2016-07-01 17:48:25 +0100]
test_should_get_new#UsersControllerTest (1467391705.83s)
ActionController::UrlGenerationError: ActionController::UrlGenerationError: No route matches {:action=>"/signup", :controller=>"users"}
test/controllers/users_controller_test.rb:6:in `block in <class:UsersControllerTest>'
test/controllers/users_controller_test.rb:6:in `block in <class:UsersControllerTest>'
Here is the routes file:
Rails.application.routes.draw do
root 'static_pages#home'
get '/signup', to: 'users#new'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
end
I have used the signup_path helper in the views as a link and they work fine.
Why is test trying to access an action called "/signup" when I have defined in the routes that "signup" should become the new action in the users controller?
Since it's a controller test (rather than feature test), your controller shouldn't care which url you visited. Therefore, get expects an action, not a path.
Change
get signup_path
to
get :new
If you want to test the route, you can modify your test case to following.
assert_generates asserts that a particular option generate a particular path.
assert_generates '/signup', {controller: 'users', action: 'new'}
assert_recognizes is the reverse of assert_generates. It asserts that a given path is recognized and routes it to a particular spot in your application.
assert_recognizes({ controller: 'users', action: 'new'}, '/signup')
The assert_routing assertion checks the route both ways: it tests that the path generates the options, and that the options generate the path. Thus, it combines the functions of assert_generates and assert_recognizes.
assert_routing({ path: 'signup', method: :get }, { controller: 'users', action: 'new' })