title: Testing custom validation method with RSpec
body: <p>I try to test validation method that check times overlap for activities.</p>
  
  <p>There are three factories(two of them inherit from activity).</p>
  
  <p>Factories:</p>
  
  <p>activities.rb</p>
  
  <pre><code>FactoryGirl.define do
      factory :activity do
        name 'Fit Girls'
        description { Faker::Lorem.sentence(3, true, 4) }
        active true
        day_of_week 'Thusday'
        start_on '12:00'
        end_on '13:00'
        pool_zone 'B'
        max_people { Faker::Number.number(2) }
        association :person, factory: :trainer
  
     factory :first do
       name 'Swim Cycle'
       description 'Activity with water bicycles.'
       active true
       day_of_week 'Thusday'
       start_on '11:30'
       end_on '12:30'
    end
  
     factory :second do
       name 'Aqua Crossfit'
       description 'Water crossfit for evereyone.'
       active true
       day_of_week 'Thusday'
       start_on '12:40'
       end_on '13:40'
       pool_zone 'C'
       max_people '30'
     end
     end
   end
  </code></pre>
  
  <p>Activities overlaps when are on same day_of_week(activity.day_of_week == first.day_of_week), on same pool_zone(activity.pool_zone == first.pool_zone) and times overlaps.</p>
  
  <p>Validation method:</p>
  
  <pre><code>  def not_overlapping_activity
      overlapping_activity = Activity.where(day_of_week: day_of_week)
                                     .where(pool_zone: pool_zone)
  
      activities = Activity.where(id: id)
      if activities.blank?
        overlapping_activity.each do |oa|
          if (start_on...end_on).overlaps?(oa.start_on...oa.end_on)
            errors.add(:base, "In this time and pool_zone is another activity.")
          end
        end
      else
        overlapping_activity.where('id != :id', id: id).each do |oa|
          if (start_on...end_on).overlaps?(oa.start_on...oa.end_on)
            errors.add(:base, "In this time and pool_zone is another activity.")
          end
        end
      end
    end
  </code></pre>
  
  <p>I wrote rspec test, but unfortunatelly invalid checks.</p>
  
  <pre><code>describe Activity, 'methods' do
    subject { Activity }
   describe '#not_overlapping_activity' do
      let(:activity) { create(:activity) }
      let(:first) { create(:first) }
  
        it 'should have a valid factory' do
          expect(create(:activity).errors).to be_empty
        end
        it 'should have a valid factory' do
          expect(create(:first).errors).to be_empty
        end
  
      context 'when day_of_week, pool_zone are same and times overlap' do
        it 'raises an error that times overlap' do
          expect(activity.valid?).to be_truthy
          expect(first.valid?).to be_falsey
          expect(first.errors[:base].size).to eq 1
        end
      end
    end
  end
  </code></pre>
  
  <p>Return:</p>
  
  <pre><code>Failure/Error: expect(first.valid?).to be_falsey
  
         expected: falsey value
              got: true
  </code></pre>
  
  <p>I can't understand why it got true. First create(:activity) should be right, but next shouldn't be executed(overlapping).
  I tried add <code>expect(activity.valid?).to be truthy</code> before <code>expect(first.valid?...</code>, but throws another error <code>ActiveRecord::RecordInvalid</code>. Could someone repair my test? I'm newbie with creation tests using RSpec.</p>
  
  <p>UPDATE:
  Solution for my problem is not create :first in test but build.
  <code>let(:first) { build(:first) }</code></p>