ScenarioResourceStep

A ScenarioResourceStep is a way to acquire a resource (or create some state) and ensure it gets released (or cleaned up) at the end of the Scenario even if normal control flow is interrupted (by an error or a failed assertion for example).

It can be implemented by a pair of Steps. One to acquire the resource and another to release it. Information can be communicated between the two steps the same way we would with any other Step, through the Session.

So, for example, in a scenario containing:

Given I setup_some_fixture_data()

where

def setup_some_fixture_data() = ScenarioResourceStep(
  title = "Set up fixture data",
  acquire = EffectStep.fromSyncE("insert data", { scenarioContext =>
    val randomId = insertData()
    scenarioContext.session.addValue("id", randomId)
  }),
  release = EffectStep.fromSync("clean up data", { scenarioContext =>
    val randomId = scenarioContext.session.getUnsafe("id")
    deleteData(randomId)
    scenarioContext.session
  })
)

we can be sure the clean up data step runs regardless of what happens after insert data.

Multiple ScenarioResourceSteps are allowed in a Scenario. In this case, the release Step of the last ScenarioResourceStep is run first, and we proceed up the Scenario.