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

时间:2018-01-04 00:24:12

标签: ruby-on-rails ruby railstutorial.org

我的测试一直在绿色,直到我达到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

我已经将这些文件用于拼写错误,但似乎无法找到正在发生的事情。感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

user.rb 文件进行拼写检查。 您为 maxmium

错误地最大

代码应该像

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