【问题标题】:Railstutorial.org 6.2.4 Format ValidationRailstutorial.org 6.2.4 格式验证
【发布时间】:2014-01-27 18:23:47
【问题描述】:

通过 Hartl 的 Railstutorial.org,我遇到了一个问题,让测试通过电子邮件进行格式验证。

我的user.rb如下:

class User < ActiveRecord::Base       
  validates :name, presence: true, length: { maximum:50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z]+\z/i
  validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }
end

user_spec.rb 是: 需要'spec_helper'

describe User do
  before do
    @user = User.new(name: "Example User", email: "user@example.com")
  end

  subject { @user }

  it { should respond_to(:name) }
  it { should respond_to(:email)}

  it { should be_valid}

  describe "when name is not present" do
    before { @user.name = " " }
    it { should_not be_valid }
  end

  describe "when email is not present" do
    before { @user.email=" "}
    it { should_not be_valid }
  end

  describe "when name is too long" do
    before { @user.name="a"*51}
    it {should_not be_valid}
  end

  describe "when email format is invalid" do
    it "should be invalid" do
      addresses = %w[user@foo,com user_at_foo.org 
                     example.user@foo. foo@bar_baz.com foo@bar+baz.com]
      addresses.each do |invalid_address|
        @user.email = invalid_address
        expect(@user).not_to be_valid
      end
    end
  end

  describe "when email format is valid" do
    it "should be valid" do
      addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
      addresses.each do |valid_address|
        @user.email = valid_address
        expect(@user).to be_valid
      end
    end
  end
end

我的错误列表如下: 失败:

  1) User
     Failure/Error: it { should be_valid}
       expected #<User id: nil, name: "Example User", email: "user@example.com",
created_at: nil, updated_at: nil> to be valid, but got errors: Email is invalid

     # ./spec/models/user_spec.rb:13:in 'block (2 levels) in <top (required)>'

  2) User when email format is valid should be valid
     Failure/Error: expect(@user).to be_valid
       expected #<User id: nil, name: "Example User", email: "user@foo.COM". cre
ated_at: nil, updated_at: nil> to be valid, but got errors. Email is invalid
     # ./spec/models/user_spec.rb:45:in 'block (4 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:43:in 'each'
     # ./spec/models/user_spec.rb:43:in 'block (3 levels) in >top (required)>'

Finished in 0.03 seconds
8 examples, 2 failures

Failed examples:

rspec ./spec/models/user_spec.rb:13 # User
rspec ./spec/models/user_spec.rb:41 # User when email format is valid should be
valid

我确信我遗漏了一些小问题(当我很难弄清楚时,它通常是小问题)。非常感谢我能得到的任何帮助。

【问题讨论】:

    标签: validation ruby-on-rails-4 format railstutorial.org


    【解决方案1】:

    教程中的具体代码如下:

    class User < ActiveRecord::Base
      validates :name,  presence: true, length: { maximum: 50 }
      VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
      validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }
    end
    

    您的VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z]+\z/i 缺少一些字母。

    【讨论】:

    • 谢谢tk。我一直在 user_spec 中寻找答案。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 2014-02-17
    相关资源
    最近更新 更多