Module: Synthra::Scenarios

Defined in:
lib/synthra/scenarios.rb

Overview

Scenario-based Test Data Fixtures

Define complex, interconnected test data scenarios that can be easily reused across tests. Think "Fixtures 2.0" - dynamic, relationship-aware, and DRY.

Examples:

Define a scenario

Synthra::Scenarios.define(:e_commerce_checkout) do
  let(:buyer) { User(role: "customer", verified: true) }
  let(:seller) { User(role: "merchant") }
  let(:laptop) { Product(seller: seller, price: 999.99) }
  let(:cart) { ShoppingCart(user: buyer, items: [laptop]) }
end

Use in tests

RSpec.describe "Checkout" do
  include_scenario :e_commerce_checkout

  it "processes payment" do
    expect(buyer.email).to be_present
    expect(cart.items).to include(laptop)
  end
end

Defined Under Namespace

Modules: MinitestHelpers, RSpecHelpers Classes: FixtureDefinition, LazyFixture, Scenario, ScenarioContext

Class Method Summary collapse

Class Method Details

.[](name) ⇒ Scenario?

Get a scenario by name

Parameters:

  • name (Symbol)

    scenario name

Returns:

  • (Scenario, nil)

    the scenario or nil



55
56
57
# File 'lib/synthra/scenarios.rb', line 55

def [](name)
  registry[name]
end

.define(name, **options) { ... } ⇒ Scenario

Define a new scenario

Parameters:

  • name (Symbol)

    scenario name

  • options (Hash)

    scenario options

Options Hash (**options):

  • :extends (Symbol)

    parent scenario to inherit from

Yields:

  • scenario definition block

Returns:



43
44
45
46
47
48
# File 'lib/synthra/scenarios.rb', line 43

def define(name, **options, &block)
  scenario = Scenario.new(name, **options)
  scenario.instance_eval(&block) if block
  registry[name] = scenario
  scenario
end

.listArray<Symbol>

List all defined scenarios

Returns:

  • (Array<Symbol>)

    scenario names



63
64
65
# File 'lib/synthra/scenarios.rb', line 63

def list
  registry.keys
end

.load_dir(dir) ⇒ Object

Load all scenarios from a directory

Parameters:

  • dir (String)

    directory path



80
81
82
83
84
# File 'lib/synthra/scenarios.rb', line 80

def load_dir(dir)
  Dir.glob(File.join(dir, "**/*.rb")).each do |file|
    load_file(file)
  end
end

.load_file(path) ⇒ Object

Load scenarios from a file

Parameters:

  • path (String)

    path to scenario file



71
72
73
74
# File 'lib/synthra/scenarios.rb', line 71

def load_file(path)
  content = File.read(path)
  instance_eval(content, path)
end

.registryObject

Registry of defined scenarios



31
32
33
# File 'lib/synthra/scenarios.rb', line 31

def registry
  @registry ||= {}
end

.reset!Object

Clear all scenarios



87
88
89
# File 'lib/synthra/scenarios.rb', line 87

def reset!
  @registry = {}
end