2016-11-09 124 views
1

我正在学习Michael Hartl的Ruby on Rails教程。 我正在处理我的用户名和电子邮件测试。Ruby on Rails错误:未定义的局部变量或方法

这里是我的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 

这里是我的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链接:https://github.com/maxkim16/RailsError请帮助。

+0

嗯我试过了,它似乎是正确的。您是否可以在添加验证时保存'User'文件? –

+0

所以现在的问题是什么?哪一组测试?删除那些不是问题的。 – sevenseacat

+0

@sevenseacat删除了旧的。 – mk4280

回答

1

仅基于您在上次测试中获得的最后一个错误,我会建议您运行bundle exec rake routes并检查是否正确定义了路由,就像它在教程中一样(可以将其复制粘贴到此处以便我们检查是否所有内容都是好)。

确认无误。

你也可以知道这个错误是否随机发生,只是一次或每次?

你是什么意思重新启动Ruby?

编辑。

检查路线的定义routes.rb如果这是正确的get 'static_pages/help'。您可以尝试将其更改为get 'static_pages/help', as: 'static_pages_help'

+0

我运行bundle exec rake路由。我重新启动计算机并再次运行Ruby。 – mk4280

+0

从我看到你有路线定义不正确,因为你可以选择你的前缀是'help',它应该是'static_pages_help'。网址格式应该是'static_pages/help' – Vekka

+0

我编辑了我的帖子,你可以试试 – Vekka

1

@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因为在耙束EXEC路线你的问题的我可以看到“static_pages#家”。 我们可以在其他页面的测试中更改get url,并将其更改为help_path等。

我希望这个答案有帮助,如果确实请给它一个绿色的勾号。干杯!

相关问题