我对“已使用电子邮件地址时的用户”的测试有问题,这是我运行测试时显示的内容
1) User when email address is already taken
Failure/Error: user_with_same_email = @user.dup
NoMethodError:
private method `initialize_dup' called for #<User:0x007f9710c7c528>
# ./spec/models/user_spec.rb:78:in `block (3 levels) in <top (required)>'
我没有意识到我定义为私人的,我不能打电话。
这是测试
describe "when email address is already taken" do
before do
user_with_same_email = @user.dup
user_with_same_email.email = @user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
这里是用户模型
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
has_secure_password
has_many :microposts, dependent: :destroy
before_save { self.email.downcase! }
before_save :create_remember_token
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
validates :name, presence: true, length: { maximum: 50}
validates :password, length: { minimum: 6 }
validates :password_confirmation, presence: true
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
谢谢