mintchoco
mintchoco

Reputation: 41

Ruby on Rails Error: undefined local variable or method

I am studying Ruby on Rails Tutorial by Michael Hartl. I am working on my user's name and email testing.

Here is my user_test.rb:

require 'test_helper'

class UserTest < ActiveSupport::TestCase

  def setup
    @user = User.new(name: "Example User", email: "[email protected]")
  end

  test "should be valid" do
    assert @user.valid? # succeds if @user.valid? returns true else returns false
  end

  test "name should be present" do
    @user.name = ""   # returns true if a user name is not a blank string.
    assert_not @user.valid?
  end

  test "email should be present" do
    @user.email = ""   # returns true if a user email is not a blank string.
    assert_not @user.valid?
  end
end

Here is my user.rb:

class User < ApplicationRecord
  validates :name, presence: true   # check name. Go to user_test.rb  line 15
  validates :email, presence: true  #    //              //           line 
end

Here is my Static_pages_ControllerTest.test.rb:

require 'test_helper'

class StaticPagesControllerTest < ActionDispatch::IntegrationTest

  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

end

I am supposed to get green when i run bundle exec rake test. However I am getting this error below:

Error:
StaticPagesControllerTest#test_should_get_home:
NameError: undefined local variable or method `static_pages_home_url' for #<StaticPagesControllerTest:0x007ff2ea5c3c28>
    test/controllers/static_pages_controller_test.rb:6:in `block in <class:StaticPagesControllerTest>'


bin/rails test test/controllers/static_pages_controller_test.rb:5

E

Error:
StaticPagesControllerTest#test_should_get_about:
NameError: undefined local variable or method `static_pages_about_url' for #<StaticPagesControllerTest:0x007ff2ec836af8>
    test/controllers/static_pages_controller_test.rb:18:in `block in <class:StaticPagesControllerTest>'


bin/rails test test/controllers/static_pages_controller_test.rb:17

E

Error:
StaticPagesControllerTest#test_should_get_help:
NameError: undefined local variable or method `static_pages_help_url' for #<StaticPagesControllerTest:0x007ff2e4a294f8>
    test/controllers/static_pages_controller_test.rb:12:in `block in <class:StaticPagesControllerTest>'


bin/rails test test/controllers/static_pages_controller_test.rb:11

Here is my bundle exec rake routes :

$bundle exec rake routes
   Prefix Verb URI Pattern          Controller#Action
users_new GET  /users/new(.:format) users#new
     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
   signup GET  /signup(.:format)    user#new

Here is my github link:https://github.com/maxkim16/RailsError Please help.

Upvotes: 1

Views: 3963

Answers (2)

Deep Mistry
Deep Mistry

Reputation: 170

@Vekka I totally agree with your answer. Also i have tried a different way in the same example by changing the test. Below i have shown only for home

test "should get home" do
 get root_path
 assert_response :success
 assert_select "title", "Ruby on Rails Tutorial Sample App"
end

I have chosen the url as root_path since in the rake bundle exec routes of your question i can see root for 'static_pages#home'. We can change the get url in the test of other pages as well by changing it to help_path and so on.

I hope this answer helps and if does please give it a green tick. Cheers!

Upvotes: 2

Vekka
Vekka

Reputation: 141

Just based on the last error that you got on last tests I would recommend you to run bundle exec rake routes and check if you defined routes correctly like it is on tutorial (you could copy paste it in here so we could check if everything is ok).

Validations are looking ok.

You could also tell if this error occurs randomly, just once or every time?

And what do you mean by restarting Ruby?

Edit.

Check definition of routes in routes.rb if this is correct get 'static_pages/help'. You can try to change it to get 'static_pages/help', as: 'static_pages_help'

Upvotes: 1

Related Questions