Created
April 20, 2017 14:00
-
-
Save TrumpClone/02b5debe52e7bc855b505a328108d56c to your computer and use it in GitHub Desktop.
amon master
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
shared_context 'stub jobs', :stub_jobs do | |
around do |spec| | |
original_method = ActiveJob::Base.method(:perform_later) | |
begin | |
ActiveJob::Base.send(:define_singleton_method, :perform_later) do |*args| | |
RSpec::Matchers::StubJobs.state.push(class: self, args: args) | |
end | |
RSpec::Matchers::StubJobs.collect(&spec) | |
ensure | |
ActiveJob::Base.send(:define_singleton_method, :perform_later, &original_method) | |
end | |
end | |
end | |
module RSpec::Matchers::StubJobs | |
module_function | |
STATE_VAR = :'__rspec/matchers/stub_jobs/state' | |
def state | |
Thread.current[STATE_VAR] | |
end | |
def state=(x) | |
Thread.current[STATE_VAR] = x | |
end | |
def collect | |
old_state = state | |
self.state = [] | |
yield | |
state | |
ensure | |
self.state = old_state.nil? ? nil : old_state + state | |
end | |
end | |
RSpec::Matchers.define :enqueue_job do |job_class| | |
supports_block_expectations | |
chain :with do |*args| | |
@args = args | |
end | |
match do |block| | |
next unless RSpec::Matchers::StubJobs.state | |
calls = | |
if block.is_a?(Proc) | |
RSpec::Matchers::StubJobs.collect(&block) | |
else | |
RSpec::Matchers::StubJobs.state | |
end | |
if @args | |
include(class: job_class, args: @args).matches?(calls) | |
else | |
include(class: job_class).matches?(calls) | |
end | |
end | |
failure_message do |block| | |
if RSpec::Matchers::StubJobs.state | |
super() | |
else | |
"include :stub_jobs context to use this matcher" | |
end | |
end | |
description do | |
if @args | |
"enqueue #{job_class}(#{@args.map(&:inspect).join ', '})" | |
else | |
"enqueue #{job_class}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment