首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Railstutorial.org 6.2.4格式验证

Railstutorial.org 6.2.4格式验证
EN

Stack Overflow用户
提问于 2014-01-28 02:23:48
回答 1查看 161关注 0票数 0

在通过Hartl的Railstutorial.org的过程中,我遇到了一个问题,使测试与电子邮件的格式验证工作。

我的user.rb如下:

代码语言:javascript
复制
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‘

代码语言:javascript
复制
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

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

代码语言:javascript
复制
  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

我确信我错过了一些次要的东西(当我很难弄清楚的时候,通常是次要的)。如果我能得到任何帮助,我将非常感激。

EN

回答 1

Stack Overflow用户

发布于 2014-01-28 17:59:55

本教程中的确切代码如下:

代码语言:javascript
复制
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缺少一些字母。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21388619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档