Module: ConflowSpec::FlowContext

Extended by:
RSpec::SharedContext
Defined in:
lib/conflow_spec/flow_context.rb

Overview

Note:

In test flows jobs will not be performed - it only tests if the jobs were enqueued with proper parameters

Defines context of the spec

  • changes Conflow::Flow class to queue jobs into special test queue

  • defines worker which collects informations about jobs to be performed

Defined Under Namespace

Classes: JobProductStruct

Instance Method Summary collapse

Instance Method Details

#_conflow_spec_queueObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Queue which collects queued jobs

See Also:



70
71
72
# File 'lib/conflow_spec/flow_context.rb', line 70

def _conflow_spec_queue
  @_conflow_spec_queue ||= Thread::Queue.new
end

#_conflow_spec_test_classObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Subclass of a described class with overriden queueing logic

See Also:



63
64
65
# File 'lib/conflow_spec/flow_context.rb', line 63

def _conflow_spec_test_class
  @_conflow_spec_test_class ||= ConflowSpec::TestFlow.build(described_class, _conflow_spec_queue)
end

#_conflow_spec_test_instanceObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Instance of the TestFlow

See Also:



56
57
58
# File 'lib/conflow_spec/flow_context.rb', line 56

def _conflow_spec_test_instance
  @_conflow_spec_test_instance ||= _conflow_spec_test_class.new
end

#allow_job(job_class, params = nil) ⇒ Object

Allows to define returned value by a job (which then can be user by hook attached to the job itself).

Examples:

class MyFlow < Conflow::Flow
  def configure(id:)
    run UpdateJob, params: { id: id }, hook: :send_notifications
  end

  def send_notifications(emails:)
    emails.each { |email| run NotificationJob, params: { email: email } }
  end
end

RSpec.describe MyFlow do
  before { allow_job(UpdateJob, id: 110).to produce(emails: ["[email protected]", "[email protected]"]) }
  subject { described_class.create(id: 110) }

  it { is_expected.to run_job(NotificationJob).with_params(email: "[email protected]") }
  it { is_expected.to run_job(NotificationJob).with_params(email: "[email protected]") }
end

Parameters:

  • job_class (Class)

    Class of the job

  • params (Object) (defaults to: nil)

    Value to be returned by job when processed



41
42
43
44
45
# File 'lib/conflow_spec/flow_context.rb', line 41

def allow_job(job_class, params = nil)
  JobProductStruct.new(job_class, params).tap do |struct|
    _conflow_spec_test_instance._conflow_spec_returns << struct
  end
end

#produce(result) ⇒ Object

Defines value returned by given job

See Also:



49
50
51
# File 'lib/conflow_spec/flow_context.rb', line 49

def produce(result)
  result
end