Class: Amigo::SpecHelpers::PerformAsyncJobMatcher
- Inherits:
-
Object
- Object
- Amigo::SpecHelpers::PerformAsyncJobMatcher
- Includes:
- RSpec::Matchers::Composable
- Defined in:
- lib/amigo/spec_helpers.rb
Instance Method Summary collapse
- #failure_message ⇒ Object
-
#initialize(job) ⇒ PerformAsyncJobMatcher
constructor
A new instance of PerformAsyncJobMatcher.
-
#matches?(given_proc) ⇒ Boolean
RSpec matcher API – return
trueif the specified job ran successfully. - #on_publish_error(err) ⇒ Object
-
#run_isolated_job(given_proc) ⇒ Object
Run
given_procin a ‘clean’ async environment, where ‘clean’ means: - Async jobs are subscribed to events - The only registered job is the matcher’s job. -
#supports_block_expectations? ⇒ Boolean
RSpec matcher API – specify that this matcher supports expect with a block.
-
#valid_proc?(given_proc) ⇒ Boolean
Return
trueif thegiven_procis a valid callable.
Constructor Details
#initialize(job) ⇒ PerformAsyncJobMatcher
Returns a new instance of PerformAsyncJobMatcher.
216 217 218 |
# File 'lib/amigo/spec_helpers.rb', line 216 def initialize(job) @job = job end |
Instance Method Details
#failure_message ⇒ Object
261 262 263 |
# File 'lib/amigo/spec_helpers.rb', line 261 def return "Job errored: %p" % [@error] end |
#matches?(given_proc) ⇒ Boolean
RSpec matcher API – return true if the specified job ran successfully.
234 235 236 237 |
# File 'lib/amigo/spec_helpers.rb', line 234 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
257 258 259 |
# File 'lib/amigo/spec_helpers.rb', line 257 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
242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/amigo/spec_helpers.rb', line 242 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.
221 222 223 |
# File 'lib/amigo/spec_helpers.rb', line 221 def supports_block_expectations? true end |
#valid_proc?(given_proc) ⇒ Boolean
Return true if the given_proc is a valid callable.
226 227 228 229 230 231 |
# File 'lib/amigo/spec_helpers.rb', line 226 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 |