require 'spec_helper' describe Campaign, 'associations' do let(:campaign){ attributes_for :campaign } it {should have_many(:videolistings) } it 'disallows duplicate listingcode' do one = Campaign.new(campaign) one.save two = Campaign.new(campaign) two.listingcode = one.listingcode expect(two.valid?).to be false end it 'shows how complete the videos are by percentage' do camp = Campaign.new(campaign) video_1 = FactoryGirl.create(:videolisting) video_2 = FactoryGirl.create(:videolisting) expect(camp.video_completion_percent).to eq(0) # no associations camp.videolistings << video_1 camp.videolistings << video_2 camp.save expect(camp.video_completion_percent).to eq(0) camp.videolistings.first.published = true camp.videolistings.first.save expect(camp.video_completion_percent).to eq(50) camp.videolistings.last.published = true camp.videolistings.last.save expect(camp.video_completion_percent).to eq(100) end end