我正在编写一个用户模型,而 RSpec 坚持我将字段留空,事实上,这些字段填充了一个完全有效的密码。这是我的 spec/models/user_spec.rb 文件:
require 'spec_helper'
describe User do
before(:each) do
@attr = {
:name => "Example User",
:email => "user@example.com",
:password => "password",
:password_confirmation => "password"
}
end
it "should create a new instance given valid attributes" do
User.create!(@attr)
end
it "should require a name" do
no_name_user = User.new(@attr.merge(:name => ""))
no_name_user.should_not be_valid
end
it "should reject names that are too long" do
long_name = "a" * 51
long_name_user = User.new(@attr.merge(:name => long_name))
long_name_user.should_not be_valid
end
it "should accept valid email addresses" do
addresses = %w[user@foo.com THE_USER@foo.bar.org first.last@foo.jp]
addresses.each do |address|
valid_email_user = User.new(@attr.merge(:email => address))
valid_email_user.should be_valid
end
end
it "should reject invalid email addresses" do
addresses = %w[user@foo,com user_at_foo.org example.user@foo.]
addresses.each do |address|
invalid_email_user = User.new(@attr.merge(:email => address))
invalid_email_user.should_not be_valid
end
end
it "should reject duplicate email addresses" do
User.create!(@attr)
user_with_duplicate_email = User.new(@attr)
user_with_duplicate_email.should_not be_valid
end
it "should reject email addresses identical up to case" do
upcased_email = @attr[:email].upcase
User.create!(@attr.merge(:email => upcased_email))
user_with_duplicate_email = User.new(@attr)
user_with_duplicate_email.should_not be_valid
end
describe "password validations" do
it "should require a password" do
User.new(@attr.merge(:password => "", :password_confirmation => "")).should_not be_valid
end
it "should require a matching password confirmation" do
User.new(@attr.merge(:password_confirmation => "invalid")).should_not be_valid
end
it "should reject short passwords" do
short = "a" * 5
hash = @attr.merge(:password => short, :password_confirmation => short)
User.new(hash).should_not be_valid
end
it "should reject long passwords" do
long = "a" * 41
hash = @attr.merge(:password => long, :password_confirmation => long)
User.new(hash).should_not be_valid
end
end
describe "password encryption" do
before(:each) do
@user = User.create!(@attr.merge(:password => "foobar", :password_confirmation => "foobar"))
end
it "should have an encrypted password attribute" do
@user.should respond_to(:encrypted_password)
end
end
end
这是我的 app/models/user.rb 文件:
class User < ActiveRecord::Base
attr_accessible :name, :email
attr_accessor :password
email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates(:name, :presence => true,
:length => { :maximum => 50 })
validates(:email, :presence => true,
:format => { :with => email_regex },
:uniqueness => { :case_sensitive => false })
validates(:password, :presence => true,
:confirmation => true,
:length => { :within => 5..41 })
end
运行 RSpec 后,我收到以下错误:
1) User should create a new instance given valid attributes
Failure/Error: User.create!(@attr)
ActiveRecord::RecordInvalid:
Validation failed: Password can't be blank
# ./spec/models/user_spec.rb:25:in `block (2 levels) in <top (required)>'
2) User should accept valid email addresses
Failure/Error: valid_email_user.should be_valid
expected #<User id: nil, name: "Example User", email: "user@foo.com", created_at: nil, updated_at: nil, encrypted_password: nil> to be valid, but got errors: Password can't be blank
# ./spec/models/user_spec.rb:43:in `block (3 levels) in <top (required)>'
# ./spec/models/user_spec.rb:41:in `each'
# ./spec/models/user_spec.rb:41:in `block (2 levels) in <top (required)>'
3) User should reject duplicate email addresses
Failure/Error: User.create!(@attr)
ActiveRecord::RecordInvalid:
Validation failed: Password can't be blank
# ./spec/models/user_spec.rb:56:in `block (2 levels) in <top (required)>'
4) User should reject email addresses identical up to case
Failure/Error: User.create!(@attr.merge(:email => upcased_email))
ActiveRecord::RecordInvalid:
Validation failed: Password can't be blank
# ./spec/models/user_spec.rb:63:in `block (2 levels) in <top (required)>'
5) User password encryption should have an encrypted password attribute
Failure/Error: @user = User.create!(@attr.merge(:password => "foobar", :password_confirmation => "foobar"))
ActiveRecord::RecordInvalid:
Validation failed: Password can't be blank
# ./spec/models/user_spec.rb:92:in `block (3 levels) in <top (required)>'
其中每一个的问题是密码字段不是空白的!它填充了“密码”这个词——它很好地介于 5 和 41 之间。在某些情况下,我将它合并到那个非常具体的测试的属性中。
谁能解释为什么这些测试失败了?