class RSpec::Stepwise::Builder

Provides DSL for steps definition and builds execution context.

Public Class Methods

new(klass) click to toggle source

@param klass [RSpec::Core::ExampleGroup]

# File lib/rspec/stepwise.rb, line 38
def initialize(klass)
  @klass = klass
  @steps_context = Context.new(klass)
  @fail_callbacks = []
end

Public Instance Methods

after(&block) click to toggle source

Runs after all steps finished.

@example

# Clears virtual mailbox after all steps.
after do
  mailbox.clear
end
# File lib/rspec/stepwise.rb, line 90
def after(&block)
  steps_context = @steps_context
  @klass.after(:all) do
    steps_context.run(&block)
  end
end
on_fail(&block) click to toggle source

Runs if any step failed. Can be executed multiple times. Execution will happen in the same order as definition order.

@example

# Outputs API logs in case of failure.
on_fail do
  puts api.execution_logs
end
# File lib/rspec/stepwise.rb, line 78
def on_fail(&block)
  @fail_callbacks << block
end
step(*args, &block) click to toggle source

Defines new step in a series. Supports the same arguments as `RSpec::Core::ExampleGroup.it`. @see RSpec::Core::ExampleGroup.it

# File lib/rspec/stepwise.rb, line 46
def step(*args, &block)
  # `it` defines method within `klass`, so Builder's
  # instance variable will not be visible there,
  # so we use local var.
  steps_context = @steps_context
  fail_callbacks = @fail_callbacks
  @klass.it(*args) do
    if steps_context.previous_failed?
      pending 'Previous step failed'
      fail
    else
      begin
        steps_context.run_step(&block)
      rescue
        fail_callbacks.each do |callback|
          steps_context.run(&callback)
        end
        raise
      end
    end
  end
end

Private Instance Methods

method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/rspec/stepwise.rb, line 99
def method_missing(name, *args, &block)
  if @klass.respond_to?(name)
    @klass.public_send(name, *args, &block)
  else
    super
  end
end
respond_to_missing?(name, include_private = false) click to toggle source
Calls superclass method
# File lib/rspec/stepwise.rb, line 107
def respond_to_missing?(name, include_private = false)
  @klass.respond_to?(name, include_private) || super
end