Class: Lopata::ScenarioBuilder

Inherits:
Object
  • Object
show all
Includes:
ActiveRecord::DSL, FactoryBot::DSL, Role::DSL
Defined in:
lib/lopata/scenario_builder.rb

Overview

Context for scenario creation.

Defined Under Namespace

Classes: OptionSet

Defining variants collapse

Defining Steps collapse

Class Method Summary collapse

Methods included from ActiveRecord::DSL

#cleanup

Methods included from Role::DSL

#as, #as_first, #without_user

Class Method Details

.define(title, metadata = {}, &block) ⇒ Object

Defines one or more scenarios.

Given block will be calculated in context of the ScenarioBuilder

Examples:

Lopata.define 'scenario' do
  setup 'test user'
  action 'login'
  verify 'home page displayed'
end

Parameters:

  • title (String)

    scenario unique title

  • metadata (Hash) (defaults to: {})

    metadata to be used within the scenario

  • block (Block)

    the scenario definition

See Also:



23
24
25
26
27
# File 'lib/lopata/scenario_builder.rb', line 23

def self.define(title,  = {}, &block)
  builder = new(title, )
  builder.instance_exec &block
  builder.build
end

Instance Method Details

#action(*steps, &block) ⇒ Object

Define action step. Action step is used for emulate user or external system action

Examples:

action do
end

# action from named shared step
action 'login'

# setup with both shared step and code block
action 'login', 'go dashboard' do
  @user.update(admin: true)
end

Parameters:

  • steps (Array<String, Symbol, Proc>)

    the steps to be runned as a part of action. String - name of shared step to be called. Symbol - metadata key, referenced to shared step name. Proc - in-place step implementation.

  • block (Block)

    The implementation of the step.



212
# File 'lib/lopata/scenario_builder.rb', line 212

define_step_method :action

#context(title, **metadata, &block) ⇒ Object

Define group of steps. The metadata for the group may be overriden Teardown steps within group will be called at the end of the group, not scenario

Examples:

context 'the task', task: :created do
  verify 'task setup'
  it 'created' do
    expect([:task]).to eq :created
  end
end

Parameters:

  • title (String)

    context title

  • metadata (Hash)

    the step additional metadata

  • block (Block)

    The implementation of the step.



256
# File 'lib/lopata/scenario_builder.rb', line 256

define_step_method :context

#diagonal(metadata_key, variants) ⇒ Object

Define diagonal for the scenario.

The scenario will be generated for all the variants of the diagonal. Each variant of diagonal will be selected for at least one scenario. It may be included in more then one scenario when other diagonal or option has more variants.

Examples:

Lopata.define 'scenario' do
  option :one, 'one' => 1, 'two' => 2
  diagonal :two, 'two' => 2, 'three' => 3
  diagonal :three, 'three' => 3, 'four' => 4, 'five' => 5
  # will generate 3 scenarios:
  # - 'scenario one two three'
  # - 'scenario two three four'
  # - 'scenario one two five'
end

Parameters:

  • metadata_key (Symbol)

    the key to access diagonal data via metadata.

  • variants (Hash{String => Object})

    variants for the diagonal. Keys are titles of the variant, values are metadata values.

See Also:



109
110
111
# File 'lib/lopata/scenario_builder.rb', line 109

def diagonal(, variants)
  @diagonals << Diagonal.new(, variants)
end

#it(title, &block) ⇒ Object

Define single validation step.

Examples:

it 'works' do
  expect(1).to eq 1
end

Parameters:

  • title (String)

    validation title

  • block (Block)

    The implementation of the step.



266
# File 'lib/lopata/scenario_builder.rb', line 266

define_step_method :it

#let(method_name, &block) ⇒ Object

Note:

The method to be called via #method_missing, so it wont override already defined methods.

Define runtime method for the scenario.

Examples:

let(:square) { |num| num * num }
it 'calculated' do
  expect(square(4)).to eq 16
end


278
279
280
281
282
# File 'lib/lopata/scenario_builder.rb', line 278

def let(method_name, &block)
  steps << Lopata::Step.new(:let) do
    execution.let(method_name, &block)
  end
end

#let!(method_name, &block) ⇒ Object

Note:

The method to be called via #method_missing, so it wont override already defined methods.

Define memorized runtime method for the scenario.

Examples:

let!(:started) { Time.now }
it 'started early' do
  first_started = started
  expect(started).to eq first_started
end


295
296
297
298
299
# File 'lib/lopata/scenario_builder.rb', line 295

def let!(method_name, &block)
  steps << Lopata::Step.new(:let) do
    execution.let!(method_name, &block)
  end
end

#metadata(hash) ⇒ Object

Define additional metadata for the scenario

Examples:

Lopata.define 'scenario' do
   key: 'value'
  it 'metadata available' do
    expect([:key]).to eq 'value'
  end
end


122
123
124
125
126
# File 'lib/lopata/scenario_builder.rb', line 122

def (hash)
  raise 'metadata expected to be a Hash' unless hash.is_a?(Hash)
  @common_metadata ||= {}
  @common_metadata.merge! hash
end

#option(metadata_key, variants) ⇒ Object

Define option for the scenario.

The scenario will be generated for all the options. If more then one option given, the scenarios for all options combinations will be generated.

Examples:

Lopata.define 'scenario' do
  option :one, 'one' => 1, 'two' => 2
  option :two, 'two' => 2, 'three' => 3
  # will generate 4 scenarios:
  # - 'scenario one two'
  # - 'scenario one three'
  # - 'scenario two two'
  # - 'scenario two three'
end

Parameters:

  • metadata_key (Symbol)

    the key to access option data via metadata.

  • variants (Hash{String => Object})

    variants for the option Keys are titles of the variant, values are metadata values.

See Also:



83
84
85
# File 'lib/lopata/scenario_builder.rb', line 83

def option(, variants)
  @options << Option.new(, variants)
end

#setup(*steps, &block) ⇒ Object

Define setup step. Setup step used for set test data.

Examples:

setup do
end

# setup from named shared step
setup 'create user'

# setup with both shared step and code block
setup 'create user' do
  @user.update(admin: true)
end

Parameters:

  • steps (Array<String, Symbol, Proc>)

    the steps to be runned as a part of setup. String - name of shared step to be called. Symbol - metadata key, referenced to shared step name. Proc - in-place step implementation.

  • block (Block)

    The implementation of the step.



189
# File 'lib/lopata/scenario_builder.rb', line 189

define_step_method :setup

#skip_when(&block) ⇒ Object

Skip scenario for given variants combination

Examples:

Lopata.define 'multiple options' do
  option :one, 'one' => 1, 'two' => 2
  option :two, 'two' => 2, 'three' => 3
  skip_when { |opt| opt.[:one] == opt.[:two] }
  it 'not equal' do
    expect(one).to_not eq two
  end
end


140
141
142
# File 'lib/lopata/scenario_builder.rb', line 140

def skip_when(&block)
  @skip_when = block
end

#teardown(*steps, &block) ⇒ Object

Define teardown step. Teardown step will be called at the end of scenario running. But it suggested to be decared right after setup or action step which require teardown.

Examples:

setup { @user = User.create! }
teardown { @user.destroy }

Parameters:

  • steps (Array<String, Symbol, Proc>)

    the steps to be runned as a part of teardown. String - name of shared step to be called. Symbol - metadata key, referenced to shared step name. Proc - in-place step implementation.

  • block (Block)

    The implementation of the step.



227
# File 'lib/lopata/scenario_builder.rb', line 227

define_step_method :teardown

#verify(*steps, &block) ⇒ Object

Define verify steps. Usually for validation shared steps inclusion

Examples:

verify 'home page displayed' # call shared step.

Parameters:

  • steps (Array<String, Symbol, Proc>)

    the steps to be runned as a part of verification. String - name of shared step to be called. Symbol - metadata key, referenced to shared step name. Proc - in-place step implementation.

  • block (Block)

    The implementation of the step.



240
# File 'lib/lopata/scenario_builder.rb', line 240

define_step_method :verify