Skip to content
This repository has been archived by the owner on Jul 30, 2019. It is now read-only.

Commit

Permalink
Unit testing SaveAuction to ensure DJ is queued/not queued
Browse files Browse the repository at this point in the history
  • Loading branch information
adelevie committed Jun 29, 2016
1 parent 4854a04 commit e865e56
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
14 changes: 7 additions & 7 deletions app/services/save_auction.rb
Expand Up @@ -5,13 +5,7 @@ def initialize(auction)

def perform
saved = auction.save

if should_schedule_auction_ended_job?(saved)
AuctionEnded
.new(auction)
.delay(run_at: auction.ended_at + 5.minutes)
.perform
end
schedule_auction_ended_job(saved)

saved
end
Expand All @@ -20,6 +14,12 @@ def perform

attr_reader :auction

def schedule_auction_ended_job(saved)
if should_schedule_auction_ended_job?(saved)
AuctionEnded.new(auction).delay(run_at: auction.ended_at).perform
end
end

def should_schedule_auction_ended_job?(saved)
saved && !auction.ended_at.nil?
end
Expand Down
36 changes: 34 additions & 2 deletions spec/services/save_auction_spec.rb
Expand Up @@ -28,9 +28,41 @@

it 'returns false' do
auction = build(:auction, title: nil)
saved = SaveAuction.new(auction).perform
SaveAuction.new(auction).perform

expect(auction).not_to be_persisted
end
end

context 'setting auction.ended_at' do
it 'queues and schedules AuctionEnded#perform' do
auction = build(:auction, ended_at: 2.days.from_now)

auction_ended_double = instance_double("AuctionEnded")
allow(AuctionEnded).to receive(:new)
.with(auction)
.and_return(auction_ended_double)

delayed_double = double
allow(delayed_double).to receive(:perform)

allow(auction_ended_double).to receive(:delay)
.with(run_at: auction.ended_at)
.and_return(delayed_double)

expect(auction_ended_double).to receive(:delay)
.with(run_at: auction.ended_at)

SaveAuction.new(auction).perform
end
end

context 'not setting auction.ended_at' do
it 'does not queue and schedule AuctionEnded#perform' do
auction = build(:auction, ended_at: nil)

expect(saved).to be(false)
expect { SaveAuction.new(auction).perform }
.not_to change { Delayed::Job.count }
end
end
end
Expand Down

0 comments on commit e865e56

Please sign in to comment.