Skip to content

Instantly share code, notes, and snippets.

@croaky
Created July 27, 2012 06:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save croaky/3186461 to your computer and use it in GitHub Desktop.
Save croaky/3186461 to your computer and use it in GitHub Desktop.
Delayed::Job example
class BidOfferedJob < Struct.new(:bid_id)
PRIORITY = 1
def self.enqueue(bid)
Delayed::Job.enqueue new(bid.id), priority: PRIORITY
end
def perform
Mailer.bid_offered(owner, bid).deliver
Activity.create activity_type: 'bid_offered', subject: bid, user: aide
Activity.create activity_type: 'bid_offered', subject: bid, user: owner
end
private
def aide
bid.user
end
def bid
Bid.find bid_id
end
def owner
bid.job.owner
end
end
require 'spec_helper'
describe BidOfferedJob do
context '.enqueue' do
before do
@bid = build_stubbed(:bid)
BidOfferedJob.enqueue @bid
end
it do
should enqueue_delayed_job('BidOfferedJob').
with_attributes(bid_id: @bid.id).
priority(1)
end
end
context '#perform' do
before do
@bid = build_stubbed(:bid)
Bid.stubs find: @bid
mailer = stub('mailer', :deliver)
Mailer.stubs bid_offered: mailer
Activity.stubs :create
BidOfferedJob.new(@bid.id).perform
end
it 'emails owner' do
Mailer.should have_received(:bid_offered).with(@bid.job.owner, @bid)
end
it 'creates a bid_offered activity for owner' do
Activity.should have_received(:create).
with(activity_type: 'bid_offered', user: @bid.job.owner, subject: @bid)
end
it 'creates a bid_offered activity for aide' do
Activity.should have_received(:create).
with(activity_type: 'bid_offered', user: @bid.user, subject: @bid)
end
end
end
module DelayedJob
module Matchers
def enqueue_delayed_job(handler)
DelayedJobMatcher.new handler
end
class DelayedJobMatcher
def initialize(handler)
@handler = handler
@attributes = {}
@priority = 0
@failure_message = ''
end
def description
"enqueue a #{@handler} delayed job"
end
def failure_message
@failure_message || <<-message.strip_heredoc
Expected #{@handler} to be enqueued as a delayed job. Try:
Delayed::Job.enqueue #{@handler}.new
message
end
def matches?(subject)
@subject = subject
enqueued? && correct_attributes? && correct_priority?
end
def priority(priority)
@priority = priority
self
end
def with_attributes(attributes)
@attributes = attributes
self
end
private
def correct_attributes?
@attributes.each do |key, value|
payload_object.send(key).should == value
end
end
def correct_priority?
if @priority == job.priority
true
else
@failure_message = <<-message.strip_heredoc
Expected priority to be #{@priority} but was #{job.priority}"
message
false
end
end
def enqueued?
payload_object.kind_of? @handler.constantize
end
def job
Delayed::Job.last
end
def payload_object
job.payload_object
end
end
end
end
@adarsh
Copy link

adarsh commented Sep 6, 2012

In your spec/spec_helper.rb, be sure to include
RSpec.configure do |config| config.include DelayedJob::Matchers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment