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 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
ジェムファイルはこちら
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