I'm using Rails 4.1.7, Rspec 3.0.4 with FactoryGirl and a postgresql ActiveRecord db.
I'm trying to test a method that finds an associated model and then updates a column in that model. The test is crude at this point, but it is making all the associations necessary (I checked in pry). When calling the method the expectation continues to return nil (as the column is nil by default but should update to a date time). Below is the code:
post_picture_spec.rb
require 'rails_helper'
RSpec.describe ModelName::PostPicture, :type => :model do
describe 'update_campaign method' do
context 'when the picture is tied to a campaign' do
before do
@campaign = FactoryGirl.create(:campaign)
@campaign.media << @picture1 = FactoryGirl.create(:picture, posted: true, time_scheduled: Date.parse('2014-07-15 18:00:00'), time_posted: Date.parse('2014-07-15 18:00:00'))
@campaign.media << @picture2 = FactoryGirl.create(:picture, posted: true, time_scheduled: Date.parse('2014-08-20 19:00:00'), time_posted: Date.parse('2014-08-20 19:00:00'))
end
it 'should update first item in campaign' do
# pending("FactoryGirl update issue")
ModelName::PostPicture.send(:update_campaign, @picture)
expect(@campaign.time_started).to eq(@picture.time_posted)
end
end
end
pictures factory
FactoryGirl.define do
factory :picture do
time_approved { Date.parse('2014-08-22 17:00:00') }
time_posted { Date.parse('2014-08-22 17:00:00') }
time_scheduled { Date.parse('2014-08-22 17:00:00') }
processed true
end
end
post_picture.rb
class ModelName::PostPicture
def self.call(id)
[collapsed]
end
private
def self.update_campaign(picture)
campaign = picture.campaign
campaign_pictures = campaign.pictures.sort_by(&:time_scheduled)
campaign.update(time_started: picture.time_posted) if campaign_pictures.first == picture
campaign.update(time_completed: picture.time_posted) if campaign_pictures.last == picture
end
end
Error:
Failure/Error: expect(@campaign.time_started).to eq(@picture1.time_posted)
expected: 2014-07-15 00:00:00.000000000 +0000
got: nil
(compared using ==)