Class: RSpec::Stepwise::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/stepwise.rb

Overview

Provides DSL for steps definition and builds execution context.

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Builder

Returns a new instance of Builder.

Parameters:

  • klass (RSpec::Core::ExampleGroup)


38
39
40
41
42
# File 'lib/rspec/stepwise.rb', line 38

def initialize(klass)
  @klass = klass
  @steps_context = Context.new(klass)
  @fail_callbacks = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



99
100
101
102
103
104
105
# 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

Instance Method Details

#after(&block) ⇒ Object

Runs after all steps finished.

Examples:

# Clears virtual mailbox after all steps.
after do
  mailbox.clear
end


90
91
92
93
94
95
# 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) ⇒ Object

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

Examples:

# Outputs API logs in case of failure.
on_fail do
  puts api.execution_logs
end


78
79
80
# File 'lib/rspec/stepwise.rb', line 78

def on_fail(&block)
  @fail_callbacks << block
end

#step(*args, &block) ⇒ Object

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

See Also:

  • Core::ExampleGroup.it


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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