我正在学习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请帮助。
嗯我试过了,它似乎是正确的。您是否可以在添加验证时保存'User'文件? –
所以现在的问题是什么?哪一组测试?删除那些不是问题的。 – sevenseacat
@sevenseacat删除了旧的。 – mk4280