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.



197
198
199
# File 'lib/amigo/spec_helpers.rb', line 197

def initialize(job)
  @job = job
end

Instance Method Details

#failure_messageObject



242
243
244
# File 'lib/amigo/spec_helpers.rb', line 242

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)


215
216
217
218
# File 'lib/amigo/spec_helpers.rb', line 215

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



238
239
240
# File 'lib/amigo/spec_helpers.rb', line 238

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



223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/amigo/spec_helpers.rb', line 223

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)


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

def supports_block_expectations?
  true
end

#valid_proc?(given_proc) ⇒ Boolean

Return true if the given_proc is a valid callable.

Returns:

  • (Boolean)


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

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