Created
January 21, 2016 00:47
-
-
Save johno/67aa09b1f29cbb0f1cb5 to your computer and use it in GitHub Desktop.
Testing asynchronous methods and behavior with Rails, DelayedJob, and Rspec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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