Michael Hartl的rails指南中有以下规格,工作正常 最高6.2.3 Length validation
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 Format validation之后,当我添加下面的代码时,它无法正常工作
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
这是Gemfile
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
答案 0 :(得分:1)
我修好了,我从这个链接获得了帮助 - Click Here
我认为这是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上并在这里放置一个链接,这就是