Hartl Rails 教程第 6 章清单 6.17 测试失败

Hartl Rails Tutorial Chapter 6 Listing 6.17 Test Fail

我的测试一直 运行 绿色,直到我到达 6.17。谁能帮我看看哪里出了问题?

这里是错误和失败:

ERROR["test_layout_links", SiteLayoutTest, 0.011797307059168816]
 test_layout_links#SiteLayoutTest (0.01s)
ArgumentError:         ArgumentError: Range unspecified. Specify the :in, 
:within, :maximum, :minimum, or :is option.
        app/models/user.rb:3:in `<class:User>'
        app/models/user.rb:1:in `<top (required)>'

 FAIL["test_email_should_not_be_too_long", UserTest, 0.04466394800692797]
 test_email_should_not_be_too_long#UserTest (0.04s)
    Expected true to be nil or false
    test/models/user_test.rb:29:in `block in <class:UserTest>'

12/12: [=============================================================] 100% 
Time: 00:00:00, Time: 00:00:00

Finished in 0.62805s
12 tests, 15 assertions, 1 failures, 1 errors, 0 skips

这是我的 user.rb 文件:

class User < ApplicationRecord
  validates :name, presence: true, length: { maximum: 50 }
  validates :email, presence: true, length: { maxmium: 255 }
end

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

  test "name should be present" do
    @user.name = "     "
    assert_not @user.valid?
  end

  test "email should be present" do
    @user.email = "     "
    assert_not @user.valid?
  end

  test "name should not be too long" do
    @user.name = "a" * 51
    assert_not @user.valid?
  end

  test "email should not be too long" do
    @user.email = "a" * 244 + "@example.com"
    assert_not @user.valid?
  end
end

这是我的 site_layout_test.rb 文件,因为它以某种方式涉及:

require 'test_helper'

class SiteLayoutTest < ActionDispatch::IntegrationTest

 test "layout links" do
   get root_path
   assert_template 'static_pages/home'
   assert_select "a[href=?]", root_path, count: 2
   assert_select "a[href=?]", help_path
   assert_select "a[href=?]", about_path
   assert_select "a[href=?]", contact_path
   get contact_path
   assert_select "title", full_title("Contact")
   get signup_path
   assert_select "title", full_title("Sign Up")
 end
end

我已经梳理了这些文件中的拼写错误,但似乎无法找到发生了什么。感谢您提供的任何帮助。

对最大 user.rb 文件进行拼写检查。 您将 maximum 拼错为 maximum

代码应该是这样的

 class User < ApplicationRecord
      validates :name, presence: true, length: { maximum: 50 }
      validates :email, presence: true, length: { maximum: 255 }
 end