Ruby on Rails Error: undefined local variable or method
Ruby on Rails Error: undefined local variable or method
我正在研究 Ruby Michael Hartl 的 Rails 教程。
我正在进行我的用户名和电子邮件测试。
这是我的 user_test.rb:
require 'test_helper'
class UserTest < ActiveSupport::TestCase
def setup
@user = User.new(name: "Example User", email: "user@example.com")
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
这是我的 user.rb:
class User < ApplicationRecord
validates :name, presence: true # check name. Go to user_test.rb line 15
validates :email, presence: true # // // line
end
这是我的 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
我应该在 运行 bundle exec rake test
时变绿。但是我在下面收到此错误:
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
这是我的 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
这是我的 github link:https://github.com/maxkim16/RailsError 请帮忙。
根据您在上次测试中遇到的最后一个错误,我建议您 运行 bundle exec rake routes
并检查您是否像教程中那样正确定义了路由(您可以复制粘贴它在这里,所以我们可以检查是否一切正常)。
验证看起来不错。
您还可以判断此错误是随机发生的,一次还是每次?
重启 Ruby 是什么意思?
编辑。
检查 routes.rb
中的路由定义是否正确 get 'static_pages/help'
。你可以试试改成get 'static_pages/help', as: 'static_pages_help'
@Vekka 我完全同意你的回答。此外,我通过更改测试在同一示例中尝试了不同的方式。下面我只显示了家庭
test "should get home" do
get root_path
assert_response :success
assert_select "title", "Ruby on Rails Tutorial Sample App"
end
我选择 url 作为 root_path 因为在你问题的 rake bundle exec routes 中我可以看到 root 对于 'static_pages#home'。
我们也可以把其他页面测试中的geturl改成help_path等等。
希望这个回答对您有所帮助,如果有帮助请打绿色勾。干杯!
我正在研究 Ruby Michael Hartl 的 Rails 教程。 我正在进行我的用户名和电子邮件测试。
这是我的 user_test.rb:
require 'test_helper'
class UserTest < ActiveSupport::TestCase
def setup
@user = User.new(name: "Example User", email: "user@example.com")
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
这是我的 user.rb:
class User < ApplicationRecord
validates :name, presence: true # check name. Go to user_test.rb line 15
validates :email, presence: true # // // line
end
这是我的 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
我应该在 运行 bundle exec rake test
时变绿。但是我在下面收到此错误:
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
这是我的 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
这是我的 github link:https://github.com/maxkim16/RailsError 请帮忙。
根据您在上次测试中遇到的最后一个错误,我建议您 运行 bundle exec rake routes
并检查您是否像教程中那样正确定义了路由(您可以复制粘贴它在这里,所以我们可以检查是否一切正常)。
验证看起来不错。
您还可以判断此错误是随机发生的,一次还是每次?
重启 Ruby 是什么意思?
编辑。
检查 routes.rb
中的路由定义是否正确 get 'static_pages/help'
。你可以试试改成get 'static_pages/help', as: 'static_pages_help'
@Vekka 我完全同意你的回答。此外,我通过更改测试在同一示例中尝试了不同的方式。下面我只显示了家庭
test "should get home" do
get root_path
assert_response :success
assert_select "title", "Ruby on Rails Tutorial Sample App"
end
我选择 url 作为 root_path 因为在你问题的 rake bundle exec routes 中我可以看到 root 对于 'static_pages#home'。 我们也可以把其他页面测试中的geturl改成help_path等等。
希望这个回答对您有所帮助,如果有帮助请打绿色勾。干杯!