undefined local variable or method `new_password_reset' - ruby-on-rails

describe "PasswordResets" do
before(:each) do
visit new_password_reset
end
it "should contain the title" do
page.should have_selector("title", text: "Password reset")
end
end
Failure/Error: visit new_password_reset
NameError:
undefined local variable or method `new_password_reset' for #
When i do a rake routes in my terminal, i get the below.
new_password_reset GET /password_resets/new(.:format) password_resets#new
Why i am getting a undefined local variable when i run my tests though the route is present.

either specify the path directly like
visit '/password-reset'
or
specify the path method which is 'new_password_reset_path' and NOT JUST 'new_password_reset'
visit new_password_reset_path

Related

ActionController::UrlGenerationError: No route matches {:action=>"/users/762146111"

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

Rspec errors in Ruby 1.8.7

I have been trying to do bundle exec rspec spec/requests/static_pages_spec.rb but everytime I try to do it I get these 9 errors:
no DRb server is running. Running in local process instead ...
FFFFFFFFF
Failures:
1) Static pages Home page should have the h1 'Sample App'
Failure/Error: visit root_path
NameError:
undefined local variable or method `root_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x1047a1cc8>
# ./spec/requests/static_pages_spec.rb:8
2) Static pages Home page should have the base title
Failure/Error: visit root_path
NameError:
undefined local variable or method `root_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x1047858e8>
# ./spec/requests/static_pages_spec.rb:13
3) Static pages Home page should not have a custom page title
Failure/Error: visit root_path
NameError:
undefined local variable or method `root_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x10477f448>
# ./spec/requests/static_pages_spec.rb:18
4) Static pages Help page should have the h1 'Help'
Failure/Error: visit help_path
ActionView::Template::Error:
undefined method `full_title' for #<#<Class:0x1069a3e98>:0x1069a0bf8>
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___963741010_2202798760'
# ./spec/requests/static_pages_spec.rb:26
5) Static pages Help page should have the title 'Help'
Failure/Error: visit help_path
ActionView::Template::Error:
undefined method `full_title' for #<#<Class:0x1069a3e98>:0x10692e508>
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___963741010_2202798760'
# ./spec/requests/static_pages_spec.rb:31
6) Static pages About page should have the h1 'About Us'
Failure/Error: visit about_path
ActionView::Template::Error:
undefined method `full_title' for #<#<Class:0x1069a3e98>:0x1068fb928>
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___963741010_2202798760'
# ./spec/requests/static_pages_spec.rb:39
7) Static pages About page should have the title 'About Us'
Failure/Error: visit about_path
ActionView::Template::Error:
undefined method `full_title' for #<#<Class:0x1069a3e98>:0x1068bc3e0>
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___963741010_2202798760'
# ./spec/requests/static_pages_spec.rb:44
8) Static pages Contact page should have the content 'Contact'
Failure/Error: visit contact_path
ActionView::Template::Error:
undefined method `full_title' for #<#<Class:0x1069a3e98>:0x106b095a8>
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___963741010_2202798760'
# ./spec/requests/static_pages_spec.rb:52
9) Static pages Contact page should have the title 'Contact'
Failure/Error: visit contact_path
ActionView::Template::Error:
undefined method `full_title' for #<#<Class:0x1069a3e98>:0x106abb740>
# ./app/views/layouts/application.html.erb:4:in `_app_views_layouts_application_html_erb___963741010_2202798760'
# ./spec/requests/static_pages_spec.rb:57
I have restarted the spork server and removed the public/index.html file and I still get the errors. Here are the results of my rake routes:
static_pages_home /static_pages/home(.:format) static_pages#home
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
GET /static_pages/home(.:format) static_pages#home
static_pages_help GET /static_pages/help(.:format) static_pages#help
static_pages_about GET /static_pages/about(.:format) static_pages#about
static_pages_contact GET /static_pages/contact(.:format) static_pages#contact
The root_path errors are because you don't have a "root" route defined. You would need to share your code to analyze the reason for the full_title not being defined. If you're following Hartl's tutorial, you might want to add a tag for that.

Hartl's Tutorial Section 5.3.2: Rails Routes

I've been following along in the tutorial exactly as it has been written. So far everything has gone without a hitch, until this section.
I was supposed to change the "GET" statements in the config/routes.rb file to the following:
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'
This was supposed to make the tests pass. They do not pass. They continue to fail with the following error as one of the 9 similar errors:
Failure/Error: visit about_path
NameError:
undefined local variable or method 'about_path' ....
I have no idea how to get this to pass so that I can move on. What did I miss? What did Hartl miss? Other people who have asked this question never got an answer that made any sense or even worked when tried.
Before anyone asks:
All versions of Rails, Ruby and other installed components are the exact same versions used in the tutorial as it is written today 2012-10-05. Everything matches the tutorial perfectly.
UPDATE: Here is the current static_pages_spec.rb file
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the h1 'Sample App'" do
visit root_path
page.should have_selector('h1', text: 'Sample App')
end
it "should have the base title" do
visit root_path
page.should have_selector('title',
text: "Ruby on Rails Tutorial Sample App")
end
it "should not have a custom page title" do
visit root_path
page.should_not have_selector('title', text: '| Home')
end
end
describe "Help page" do
it "should have the h1 'Help'" do
visit help_path
page.should have_selector('h1', text: 'Help')
end
it "should have the title 'Help'" do
visit help_path
page.should have_selector('title',
text: "Ruby on Rails Tutorial Sample App | Help")
end
end
describe "About page" do
it "should have the h1 'About'" do
visit about_path
page.should have_selector('h1', text: 'About Us')
end
it "should have the title 'About Us'" do
visit about_path
page.should have_selector('title',
text: "Ruby on Rails Tutorial Sample App | About Us")
end
end
describe "Contact page" do
it "should have the h1 'Contact'" do
visit contact_path
page.should have_selector('h1', text: 'Contact')
end
it "should have the title 'Contact'" do
visit contact_path
page.should have_selector('title',
text: "Ruby on Rails Tutorial Sample App | Contact")
end
end
end
Rake Routes results:
root / static_pages#home
help /help(.:format) static_pages#help
about /about(.:format) static_pages#about
contact /contact(.:format) static_pages#contact
Try changing in your routes.rb your line to:
match '/about', to: 'static_pages#about', as: 'about'
and also you should reload Spork in order to changes apply.
You can also add:
load "#{Rails.root}/config/routes.rb"
into your Spork.each_run block in order to reload routes each time you run Spork.
Reloading spork worked for me.
Hi try to add this on your spec.
describe "Static pages" do
include Rails.application.routes.url_helpers
.......
I think you should type in the browser
http://localhost:3000/
instead of http://localhost:3000/static_pages/home to get the home page for example.
I think the problem is not the code but rather how to access the page. In the sections prior to 5.3.2 in the tutorial, you needed to visit localhost:3000/static_pages/home to access the homepage. After you follow instructions in 5.3.2, you only need to type http://localhost:3000/.
I just encountered this error, and had some help fixing this. I had mistakenly added the signin_path and signup_path on the layouts/_header.html.erb and static_pages/home.html.erb, respectively. This had EVERY page confused as to what the signin_path was.
The other issue I had made an error on was the root path. If you run 'rm public/index.html' from your sample app directory, 'root 'static_pages#home' in routes.rb, should work. The problem is that things in the public directory override any other part of your app as you customize it. Hope this helps you like it helped me :)
Named routes are not available in specs by default. Add the following code to the spec_helper.rb file:
RSpec.configure do |config|
config.include Rails.application.routes.url_helpers
end

Rspec no route matches

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.

Integration Testing With Ruby on Rails Tutorial Issue

I'm trying to follow the ruby on rails tutorial and impliment the Integration Testing.
I first run the command: bundle exec rspec spec/
And it tells me all but one of my sixteen tests passes. Here is the part where I think the issue is:
require 'spec_helper'
describe "LayoutLinks" do
it "should have the right links on the layout" do
visit root_path
click_link "Help"
response.should have_selector('title', :content => "Help")
click_link "Contact"
response.should have_selector('title', :content => "Contact")
click_link "Home"
response.should have_selector('title', :content => "Home")
click_link "Sign up now!"
response.should have_selector('title', :content => "Sign Up")
click_link "About"
response.should have_selector('title', :content => "About")
end
end
As a result I get the following:
Failure/Error: response.should have_selector('title', :content => "Help")
expected following output to contain a <title>Help</title> tag:
#home.html.erb page's source shown
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Ruby on Rails Tutorial Sample App | Home</title>
#The line above is why the test fails.
#It is loading home.html.erb instead of help.html.erb
.
.
.
# ./spec/requests/layout_links_spec.rb:34:in `block (2 levels) in <top (required)>'
I can move around the order of the tests and it is always the top test that fails. This makes me believe there is something wrong here and not with the actual rails code. I can also go to the demo website and the links work and they go to the correct pages. I have looked at the other issues other people have had with this and I can't seem to find anyone who is having the same issues. How can I go about trouble shooting this?
Update:
noahc:sample_app noahc$ rake routes
users_new GET /users/new(.:format) {:controller=>"users", :action=>"new"}
signup /signup(.:format) {:controller=>"users", :action=>"new"}
contact /contact(.:format) {:controller=>"pages", :action=>"contact"}
about /about(.:format) {:controller=>"pages", :action=>"about"}
help /help(.:format) {:controller=>"pages", :action=>"help"}
/help(.:format) {:controller=>"pages", :action=>"help"}
root / {:controller=>"pages", :action=>"home"}
pages_home GET /pages/home(.:format) {:controller=>"pages", :action=>"home"}
pages_contact GET /pages/contact(.:format) {:controller=>"pages", :action=>"contact"}
pages_about GET /pages/about(.:format) {:controller=>"pages", :action=>"about"}
pages_help GET /pages/help(.:format) {:controller=>"pages", :action=>"help"}
Noah, I would recommend splitting that test up into multiple tests, one for each title that you would like to test. That will make it a little easier for you to pinpoint exactly what's wrong. Something like this:
it "should have a link to 'Help'" do
visit root_path
response.should have_selector('a', :href => help_path, :content => "Help")
click_link "Help"
response.should have_selector('title', :content => 'Help')
end
it "should have a link to 'Contact'" do
visit root_path
response.should have_selector('a', :href => contact_path, :content => "Contact")
click_link "Help"
response.should have_selector('title', :content => 'Contact')
end
etc... This will make it easier to pinpoint exactly what is going on, and where you're experiencing a problem. Also, what did it say was being displayed in the page's source? You cut off the error message before it explained what really happened in your paste snippet... Look inside what the code that is actually being returned says to see what the title is and how/why it is differing from what rspec is expecting.
I don't use rspec, but could be because "Home" != "Ruby on Rails Tutorial Sample App | Home"
The contents do not match at all, so the test fails.
Also, check your help link in your source to be sure it is in fact going where you think it is...

Resources