Skip to content

Instantly share code, notes, and snippets.

@johno
Created January 21, 2016 00:47
Show Gist options
  • Save johno/67aa09b1f29cbb0f1cb5 to your computer and use it in GitHub Desktop.
Save johno/67aa09b1f29cbb0f1cb5 to your computer and use it in GitHub Desktop.
Testing asynchronous methods and behavior with Rails, DelayedJob, and Rspec
class SomeJob
attr_accessor :some_model
def initialize(some_model_id)
self.some_model = SomeModel.find(some_model_id)
end
def perform
some_model.do_stuff
end
end
class SomeModel < ActiveRecord::Base
after_save :delay_do_stuff!
def do_stuff
# ...
end
private
def delay_do_stuff
Delayed::Job.enqueue(id) if stuff
end
end
require 'spec_helper'
describe SomeModel, type: :model do
it 'delays do_stuff after save' do
expect(some_model).to receive(:delay_do_stuff)
some_model.run_callbacks(:save)
end
describe '#do_stuff' do
# Test do_stuff behavior...
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment