Going through Hartl's Rails tutorial and on Chapter 5.4.2 I am not able to get a certain test to pass.
Here is the error:
1) UserPages GET /user_pages works! (now write some real specs)
Failure/Error: get user_pages_index_path
NameError:
undefined local variable or method `user_pages_index_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007f9106e2cce8>
# ./spec/requests/user_pages_spec.rb:7:in `block (3 levels) in <top (required)>'
Here is the test in user_pages_spec.rb:
require 'spec_helper'
describe "UserPages" do
describe "GET /user_pages" do
it "works! (now write some real specs)" do
# Run the generator again with the
# --webrat flag if you want to use webrat methods/matchers
get user_pages_index_path
response.status.should be(200)
end
end
end
Here is the routes file:
get "users/new"
root 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
match 'static_pages/home', to: 'static_pages#home', via: 'get'
Any input is appreciated
Sorry about the formatting. In the very beginning of that section is a spec file: spec/requests/user_pages_spec.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "signup page" do
before { visit signup_path }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end
end
and I am getting the following error:
Failure/Error: get user_pages_index_path
NameError: undefined local variable or method `user_pages_index_path' for #
Here is the rake routes output:
Prefix Verb URI Pattern Controller#Action
users_new GET /users/new(.:format) users#new
root GET / static_pages#home
signup GET /signup(.:format) users#new
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
static_pages_home GET /static_pages/home(.:format) static_pages#home
Just remove the contents at "user_pages_spec.rb"
cause there is no such method called user_pages_index_path
so, here is what it should be
user_pages_spec.rb
require 'spec_helper'
describe "User pages" do
subject { page }
describe "signup page" do
before { visit signup_path }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end
end
Also, At /config/routes.rb file
SampleApp::Application.routes.draw do
get "users/new"
root 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
.
.
.
end
I was having same issue and I just commented out the "user_pages_spec.rb" file. When I did that I passed all the tests. Is this the correct answer, I don't know but as I stated it allows for the tests to pass.
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
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' })
Hi so I'm working on chapter 8.2.6 of Hartl's Rails tutorial
My application fails a test that I wrote for testing the 'sign out' link.
Here is a copy of my command prompt
C:\Sites\sample_app>bundle exec rspec spec/
.............F.................................
Failures:
1) Authentication signin with valid information followed by signout
Failure/Error: before { click_link "Sign out" }
ActionController::RoutingError:
uninitialized constant SessionController
# ./spec/requests/authenticate_pages_spec.rb:35:in `block (5 levels) in <to
p (required)>'
Finished in 1.17 seconds
47 examples, 1 failure
Failed examples:
rspec ./spec/requests/authenticate_pages_spec.rb:36 # Authentication signin with
valid information followed by signout
Here is the relevant part of the test code (spec/requests/authenticate_pages_spec.rb)
describe "followed by signout" do
before { click_link "Sign out" }
it { should have_link('Sign in') }
end
app/controllers/sessions_controller.rb
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to user
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
sign_out
redirect_to root_url
end
end
config/routes
SampleApp::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
root 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'session#destroy', via: 'delete'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
Your indicator here is uninitialized constant SessionController.
Your controller name is plural, SessionsController
If you look at your routes.rb file, you will notice your typo.
match '/signout', to: 'session#destroy', via: 'delete'
which should look like this:
match '/signout', to: 'sessions#destroy', via: 'delete'
I have started working on Chapter 8 of the famous Rails tutorial. I think I followed the instructions closely and defined the following routes:
SampleApp::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
root 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
The session controller (/controllers/sessions_controller.rb) is defined as follows:
class SessionsController < ApplicationController
def new
end
def create
end
def destroy
end
end
In spec/requests/authentication_pages_spec.rb I have created the following test:
require 'spec_helper'
describe "Authentication" do
subject {page}
describe "signin page" do
before { visit signin_path}
it { should have_content('Sign in')}
it { should have_title('Sign in')}
end
end
The test causes the following errors:
Failures:
1) Authentication signin page
Failure/Error: before { visit signin_path}
NameError:
undefined local variable or method `signin_path' for #
<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007f98dec05fe8>
# ./spec/requests/authentication_pages_spec.rb:7:in `block (3 levels) in <top (required)>'
2) Authentication signin page
Failure/Error: before { visit signin_path}
NameError:
undefined local variable or method `signin_path' for #
<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007f98dec5cf50>
# ./spec/requests/authentication_pages_spec.rb:7:in `block (3 levels) in <top (required)>'
It seems the signin_path named route is not recognised even though it is defined in routes.rb. I replaced that named route with one of the others (signup_path) and the problem disappeared. So, it is something about this particular named route. Can you tell what the problem is?
rake routes produces the following output:
sb7904313:sample_app nnikolo$ rake routes
Prefix Verb URI Pattern Controller#Action
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
root GET / static_pages#home
signup GET /signup(.:format) users#new
signin GET /signin(.:format) sessions#new
signout DELETE /signout(.:format) sessions#destroy
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
I also re-started the server and it did not solve the problem (I made a post to the contrary but I was wrong).
Try this instead. You can access your route names by the command rake routes. Since it's coming from your sessions controller, by default the route is probably something like new_session_path. To change it, you need to specify what you what to change it to in your routes with as: new_name
match '/signin', to: 'sessions#new', via: 'get', as: 'signin'
Try:--
get "signin" => "sessions#new", :as => "signin"
resources :sessions
Use the route as new_session_path or signin_path
See this thread: undefined method `visit' when using RSpec and Capybara in rails
You possibly did not have the line config.include Capybara:DSL in your spec_helper.rb and something about putting the tests inside rspec/features/ due to the latest changes in Capybara.
I was wrong about the re-start solving the problem so retract this post.
==EDIT I found the culprit for this erratic behaviour of rspec: rails does not seem to empty the cache between tests (which is, in my opinion, a scary bug). By default it fails to re-read the files and thus may ignore changes that have occurred. I put more details here: Rails tutorial: Rails tutorial: undefined method
I am following Michale Hartl's tutorial and I am currently at this step:
Listing 5.23. Adding a mapping for the root route. config/routes.rb
SampleApp::Application.routes.draw do
root to: 'static_pages#home'
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
end
I have copied his exact coding to my config/routes.rb and continue to get a Routing Error:
Routing Error
No route matches [GET] "/static_pages/home.html".
--
Not sure what to do at this point to get my home routed as the index, and to be able to use root_path to link back to 'Home'.
Remove ../static_pages/.. from the URL you've entered in the browser.
this code is correct
SampleApp::Application.routes.draw do
root :to => 'static_pages#home'
get '/help' => 'static_pages#help'
get '/about' => 'static_pages#about'
get '/contact' => 'static_pages#contact'
end
You do not have to delete the index page, the rspec test in the tutorial is not wrong, this is the updated code with current available methods
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/'
page.should have_content('Sample App')
end
end
This is using ruby -v ruby 2.0.0p47 and rails -v Rails 4.0.3
you can also get a failure if your using before { visit help_path }, using the Rails Console and checking the path app.help_path confirms 'before { visit help_path } is correct but if this code is not in spec/rspec_helpers.rb then the test will fail because it cannot find the path
Named routes should work if you put the following in rspec_helper.rb:
Rspec.configure do |config|
config.include Rails.application.routes.url_helpers
...
end
SampleApp::Application.routes.draw do
root :to => 'static_pages#home'
get '/help' => 'static_pages#help'
get '/about' => 'static_pages#about'
get '/contact' => 'static_pages#contact'
end
this code is right.
You need to delete or rename the public/index.html and then it will work.