white-bread icon indicating copy to clipboard operation
white-bread copied to clipboard

Scenario Start/End callbacks not fired in subcontexts

Open mgwidmann opened this issue 7 years ago • 3 comments

Something like the following does not work:

defmodule GlobalContext do
  use WhiteBread.Context

  scenario_starting_state fn state ->
    Application.ensure_all_started(:hound)
    Hound.start_session
    state
  end

  scenario_finalize fn _state ->
    Hound.end_session
  end

  # Global given/then/and/ect you want to share with all contexts
end

defmodule MyContext do
  use WhiteBread.Context

  subcontext GlobalContext
end

It looks like here that steps are respected: https://github.com/meadsteve/white-bread/blob/master/lib/white_bread/context/setup.ex#L9

But below the scenario start/end events are not (or maybe I'm wrong).

Either way I can't get the code in those events to start hound without coping and pasting it into each file. Also since the contexts are loaded first it seems, I can't do a use HoundSetup either...

mgwidmann avatar Dec 02 '16 20:12 mgwidmann

@mgwidmann this was initially deliberate as I thought it'd be confusing to have lots of callbacks potentially triggering (and the required rules around ordering).

How I would implement this would be using plain functions/modules:

defmodule GlobalContextHelper do

  def start(state) do
    Application.ensure_all_started(:hound)
    Hound.start_session
    state
  end

  def stop(_state)
    Hound.end_session
  end
end

defmodule MyContext do
  use WhiteBread.Context
  
  scenario_starting_state fn state ->
    GlobalContextHelper.start(state)
  end

  scenario_finalize fn state ->
    GlobalContextHelper.stop(state)
  end
end

It'll mean a little boiler plate in each executed context but it'll make the order of what's happening very explicit.

Do you think it would make the intended use clearer if I renamed subcontext to import_steps_from so you'd have something like:

defmodule MyContext do
  use WhiteBread.Context
  import_steps_from GlobalContext
end

meadsteve avatar Dec 04 '16 10:12 meadsteve

Yeah, that would make it clearer for sure. I'd like though to not have to define a scenario_starting_state and scenario_finalize for each context and do something like use GlobalContextHelper, but it wont work because its not compiled yet. Would I have to put that into test/support (with the modification that phoenix adds to compile those in the test env)?

I'd prefer to not have to move code there since theres no integration testing in the test folder, seems odd to have that code compiled and available.

mgwidmann avatar Dec 05 '16 18:12 mgwidmann

To be honest I never really anticipated there being that many contexts doing setup and finalise. The contexts are inspired by this cucumber implementation: http://behat.org/en/latest/user_guide/configuration/suites.html and when using this I've not normally seen more than a few top level contexts. Normally things like a WebContext and an APIContext (maybe a CLI context). Or a few different contexts for different user types of the system.

meadsteve avatar Dec 06 '16 08:12 meadsteve