Class: Amigo::SpecHelpers::PerformAsyncJobMatcher

Inherits:
Object
  • Object
show all
Includes:
RSpec::Matchers::Composable
Defined in:
lib/amigo/spec_helpers.rb

Instance Method Summary collapse

Constructor Details

#initialize(job) ⇒ PerformAsyncJobMatcher

Returns a new instance of PerformAsyncJobMatcher.



190
191
192
# File 'lib/amigo/spec_helpers.rb', line 190

def initialize(job)
  @job = job
end

Instance Method Details

#failure_messageObject



235
236
237
# File 'lib/amigo/spec_helpers.rb', line 235

def failure_message
  return "Job errored: %p" % [@error]
end

#matches?(given_proc) ⇒ Boolean

RSpec matcher API – return true if the specified job ran successfully.

Returns:

  • (Boolean)


208
209
210
211
# File 'lib/amigo/spec_helpers.rb', line 208

def matches?(given_proc)
  return false unless self.valid_proc?(given_proc)
  return self.run_isolated_job(given_proc)
end

#on_publish_error(err) ⇒ Object



231
232
233
# File 'lib/amigo/spec_helpers.rb', line 231

def on_publish_error(err)
  @error = err
end

#run_isolated_job(given_proc) ⇒ Object

Run given_proc in a ‘clean’ async environment, where ‘clean’ means:

  • Async jobs are subscribed to events

  • The only registered job is the matcher’s job



216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/amigo/spec_helpers.rb', line 216

def run_isolated_job(given_proc)
  unless Amigo.synchronous_mode
    warn "publish matcher used without synchronous_mode (use :async test metadata)"
    return false
  end

  state = {on_error: self.method(:on_publish_error), subscribers: [], jobs: [@job]}
  Amigo::SpecHelpers.snapshot_async_state(state) do
    Amigo.install_amigo_jobs
    given_proc.call
  end

  return @error.nil?
end

#supports_block_expectations?Boolean

RSpec matcher API – specify that this matcher supports expect with a block.

Returns:

  • (Boolean)


195
196
197
# File 'lib/amigo/spec_helpers.rb', line 195

def supports_block_expectations?
  true
end

#valid_proc?(given_proc) ⇒ Boolean

Return true if the given_proc is a valid callable.

Returns:

  • (Boolean)


200
201
202
203
204
205
# File 'lib/amigo/spec_helpers.rb', line 200

def valid_proc?(given_proc)
  return true if given_proc.respond_to?(:call)

  warn "`perform_async_job` was called with non-proc object #{given_proc.inspect}"
  return false
end