0

我有以下来自 Michael Hartl 的 rails 教程的规范,并且 在6.2.3 长度验证之前它都可以正常工作

require 'spec_helper'

describe User do

  before { @user = User.new(name: "Example User", email: "user@example.com")}

  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
end

6.2.4 格式验证之后,当我添加下面的代码时,它不起作用

describe "when email format is invalid" do
    it "should be invalid" do
      addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
      addresses.each do |invalid_address|
        @user.email = invalid_address
        @user.should_not 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
        @user.should be_valid
      end
    end
  end

这是完整的文件

require 'spec_helper'

describe User do

  before { @user = User.new(name: "Example User", email: "user@example.com")}

  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.]
      addresses.each do |invalid_address|
        @user.email = invalid_address
        @user.should_not 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
        @user.should be_valid
      end
    end
  end
end

这是错误日志,知道如何解决吗?

bundle exec rspec spec/
....................F.....F

Failures:

  1) User 
     Failure/Error: it { should be_valid }
       expected valid? to return true, got false
     # ./spec/models/user_spec.rb:12:in `block (2 levels) in <top (required)>'

  2) User when email format is valid should be valid
     Failure/Error: @user.should be_valid
       expected valid? to return true, got false
     # ./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 1.4 seconds
27 examples, 2 failures

Failed examples:

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

Randomized with seed 60820

这是宝石文件

cat Gemfile
source 'https://rubygems.org'

gem 'rails', '3.2.13'
gem 'bootstrap-sass', '2.3.1.0'
gem 'bcrypt-ruby', '3.0.1'
gem 'faker', '1.0.1'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'
gem 'jquery-rails', '2.2.1'

group :development, :test do
  gem 'sqlite3', '1.3.7'
  gem 'rspec-rails', '2.11.0'
  gem 'guard-rspec', '1.2.1'
  gem 'guard-spork', '1.2.0'
  gem 'spork', '0.9.2'
end

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails', '3.2.6'
  gem 'coffee-rails', '3.2.2'
  gem 'uglifier', '2.0.1'
end

group :test do
  gem 'capybara', '1.1.2'
  gem 'factory_girl_rails', '4.1.0'
  gem 'cucumber-rails', '1.2.1', :require => false
  gem 'database_cleaner', '0.7.0'
  # gem 'launchy', '2.1.0'
  # gem 'rb-fsevent', '0.9.1', :require => false
  # gem 'growl', '1.0.3'
end

和模型文件

$ cat app/models/user.rb 
class User < ActiveRecord::Base
  attr_accessible :email, :name

  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
4

1 回答 1

1

我修好了,我从这个链接得到了帮助 -点击这里

我认为这是我VALID_EMAIL_REGEX的错

VALID_EMAIL_REGEX = /\A[w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

实际是:

VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

下次我将代码放在github上并在这里放一个链接,那就是

于 2013-05-07T22:33:59.080 回答