0
votes

I have an Account model with has_secure_password (that automagically creates 2 attr_accessors: password and password_confirmation, required on create). I'd like to avoid to specify those fields when I update an account object, so I'm trying to create a test with RSpec and factoryGirl like this:

describe Account do
    before do
        @account = FactoryGirl.build(:account) # this does not save the object
    end

    describe "password is not present (on update)" do
        before do
            @account.save
            @account.name = 'updated without specifying the password field'
        end
        it "should be valid" do
            should be_valid
        end
    end
end

But, I end up with the error:

   Account password is not present (on update) should be valid
      Failure/Error: should be_valid
        expected valid? to return true, got false
      # ./spec/models/account_spec.rb:48:in `block (3 levels) in <top (required)>'

I cannot use the FactoryGirl.create(:account) because the before block gets executed before many other tests and the model validates the uniqueness of the email field.


If I use FactoryGirl.create(:account) within the initial before block (I'm now using a sequence generator to create a unique email address)... this test fails:

describe "email is already taken" do
    before do
      same_user = @account.dup
      same_user.email = @account.email.upcase # let's test against case sensitive strings
      same_user.save
    end

    it { should_not be_valid }
  end

These are some validations inside the Account model:

 validates :name, :surname, presence: true
  validates :email, presence: true,
                    uniqueness: { case_sensitive: false },
                    format: { with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i }
  validates :birthday, presence: true

  # Password validations
  validates :password, presence: true, :on => :create
  validates :password, length: { minimum: 4, maximum: 20 }, allow_blank: true
  validates :password_confirmation, presence: true, :unless => lambda { self.password.blank? }
1
Why don't you use a sequence generator to generate a unique email for each object?usha
You're right. I've added the code. Thank youMich Dart

1 Answers

1
votes

this will generate a unique email address every time you use factory to create/build an account

sequence(:unique_email) {|n| "email#{n}@example.com" }
factory :account do
  email { generate(:unique_email) }
end

You don't have to save the object to check for validity

describe "email is already taken" do
    before do
      same_user = @account.dup
      same_user.email = @account.email.upcase # let's test against case sensitive strings
      same_user.valid?
    end

    it { should_not be_valid }
  end