3

I have a problem with the test of "User when email address is already taken", here is what it shows when I run the test

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)>'

I don't realize what I am defining as private and I can't call.

Here is the test

    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

and here the user model

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

Thank you

agusgambina
  • 6,229
  • 14
  • 54
  • 94

3 Answers3

1

It was a bug in Rails 3.2.12 with ruby 2. Take a look on this https://github.com/rails/rails/issues/9417. Switching to Rails 3.2.15 should solve your problem.

Marek Takac
  • 3,002
  • 25
  • 30
  • Thank you for your reply. I was using gem 'rails', '3.2.3' and gem 'rspec-rails', '2.9.0'. I change it for gem 'rails', '3.2.15' and gem 'rspec-rails', '2.10.0' I made and update and it worked. Thank you. – agusgambina Oct 29 '13 at 16:56
  • 1
    Having this problem as well, with Ruby 2.1.0, Rails 4.0.2, rspec 2.14.1. If Rails 3.2.15 fixed it, it seems to be back in 4.0.2 – jackr Jan 10 '14 at 22:58
1

This is really a comment to "it seems to be back in 4.0.2 – jackr Jan 10 at 22:58":

What I've done is force it to be public:

@my_object.errors.class_eval do
  def initialize_dup(other) # :nodoc:
     @messages = other.messages.dup
     super
  end
end
@my_object.errors.initialize_dup(another_object.errors)

I'd appreciate some up votes so that I can get enough points so that stackoverflow will allow me to comment :)

0

According to the accepted answer in my similar ticket, Rails 4 Validating email uniqueness without case_sensitive, this seems to be an irreconcilable trade-off between performance and case-sensitive validation for MySQL. There are links there to two GitHub issues that show some flip-flopping on the implementation, but there doesn't appear to be any fully satisfactory solution possible. Several work-arounds are suggested there.

Community
  • 1
  • 1
jackr
  • 1,407
  • 1
  • 14
  • 29