Completely describe Conference behaviour with specs

Add missing specs for the Conference model and remove unnecessary
functionality. Also introduce a Hall factory.
This commit is contained in:
Petko Bordjukov 2014-11-04 23:02:28 +02:00
parent 6330940a34
commit 6fb3532f01
4 changed files with 77 additions and 10 deletions

View File

@ -8,21 +8,13 @@ class Conference < ActiveRecord::Base
translates :title, :description translates :title, :description
has_many :tracks, -> { order('id asc') }, dependent: :destroy has_many :tracks
has_many :events, through: :tracks has_many :halls
has_many :candidate_speakers, through: :events
has_many :halls, dependent: :destroy
accepts_nested_attributes_for :tracks, :halls, reject_if: :all_blank, allow_destroy: true accepts_nested_attributes_for :tracks, :halls, reject_if: :all_blank, allow_destroy: true
before_save :close_all_other_calls_for_papers
private private
def close_all_other_calls_for_papers
Conference.where.not(id: self.id).update_all call_for_papers_open: false
end
def start_date_is_before_end_date def start_date_is_before_end_date
if start_date.present? and end_date.present? and start_date > end_date if start_date.present? and end_date.present? and start_date > end_date
errors.add(:end_date, :cannot_be_before_start_date) errors.add(:end_date, :cannot_be_before_start_date)

8
spec/factories/halls.rb Normal file
View File

@ -0,0 +1,8 @@
# Read about factories at https://github.com/thoughtbot/factory_girl
FactoryGirl.define do
factory :hall do
name { |n| "Hall#{n}" }
conference
end
end

View File

@ -4,6 +4,7 @@ FactoryGirl.define do
factory :track do factory :track do
name { |n| "Track#{n}" } name { |n| "Track#{n}" }
color '#000000' color '#000000'
description 'Some description'
conference conference
end end
end end

View File

@ -50,4 +50,70 @@ RSpec.describe Conference, :type => :model do
it 'is invalid when the end date is before the start date' do it 'is invalid when the end date is before the start date' do
expect(build(:conference, start_date: '2014-07-29 21:29:13', end_date: '2014-07-28 01:00:00')).to have_error_on :end_date expect(build(:conference, start_date: '2014-07-29 21:29:13', end_date: '2014-07-28 01:00:00')).to have_error_on :end_date
end end
describe 'tracks association' do
let(:conference) { build :conference }
let(:track) { build :track }
before do
conference.save
track.conference = conference
track.save
end
it 'links the conference to its tracks' do
expect(conference.tracks).to include track
end
end
describe 'halls association' do
let(:conference) { build :conference }
let(:hall) { build :hall }
before do
conference.save
hall.conference = conference
hall.save
end
it 'has many associated halls' do
expect(conference.halls).to include hall
end
end
it 'accepts nested attributes for tracks' do
track_attributes = build(:track, conference: nil).attributes
conference = create :conference
expect { conference.update tracks_attributes: [track_attributes]}.to change { conference.tracks.count }.by 1
end
it 'rejects nested attributes for tracks when they are all blank' do
conference = create :conference
expect { conference.update tracks_attributes: [{'name' => '', 'description' => ''}]}.to_not change { conference.tracks.count }
end
it 'allows destruction of tracks by nested attributes' do
conference = create :conference
track_attributes = create(:track, conference: conference).attributes
track_attributes['_destroy'] = 1
expect { conference.update tracks_attributes: [track_attributes]}.to change { conference.tracks.count }.by(-1)
end
it 'accepts nested attributes for halls' do
hall_attributes = build(:hall, conference: nil).attributes
conference = create :conference
expect { conference.update halls_attributes: [hall_attributes]}.to change { conference.halls.count }.by 1
end
it 'rejects nested attributes for halls when they are all blank' do
conference = create :conference
expect { conference.update halls_attributes: [{'name' => '', 'description' => ''}]}.to_not change { conference.halls.count }
end
it 'allows destruction of halls by nested attributes' do
conference = create :conference
hall_attributes = create(:hall, conference: conference).attributes
hall_attributes['_destroy'] = 1
expect { conference.update halls_attributes: [hall_attributes]}.to change { conference.halls.count }.by(-1)
end
end end