Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Before-Step hook not invoked for multisteps #486

Closed
wichert opened this issue Jul 21, 2022 · 2 comments · Fixed by #568 · May be fixed by #491
Closed

Before-Step hook not invoked for multisteps #486

wichert opened this issue Jul 21, 2022 · 2 comments · Fixed by #568 · May be fixed by #491

Comments

@wichert
Copy link
Contributor

wichert commented Jul 21, 2022

I have a before-Step hook to replace variables in steps. You can use it do things like this:

Scenario: Demonstrate variables
  Given I set "name" to "Zaphod"
  I login as ${name}

Normally variables are set by other steps, and they provide a convenient way to pass some context around between API calls. One use case is to test an API where I need to create a parent and a child object. I can do that using variables like so:

Scenario: Test creation of books
  Given I create an author named "Jane Doe"
  When I create a book "Intro to godog" for author ${author_id}
  Then the API returned succeeded

Doing this many times becomes tedious, especially when you need to go down more levels. To solve that I want to leverage a multistep:

ctx.Step("there exists an author with a book", func(context.Context) godog.Steps {
    return godog.Steps{
        `I create an author named "Jane Doe"`,
        `I create a book "Intro to godog" for author ${author_id}`,
        "the API returned succeeded",
    }
}

so I can just start my tests like this:

Scenario: Test creation of books
  Given there exists an author with a book
  ...

This is failing, because my StepContext.Before is never invoked for steps returned by a multistep.

📦 Which tool/library version are you using?

godog v0.12.4

@wichert
Copy link
Contributor Author

wichert commented Jul 25, 2022

@vearutop I would like to contribute a fix for this, but I'm not sure how best to approach this. Do you have any hints?

@vearutop
Copy link
Member

vearutop commented Jul 25, 2022

I was briefly looking at this issue today in the morning and can confirm it exists.

Here is where BeforeStep is invoked

godog/suite.go

Line 136 in b2672bb

ctx, err = s.runBeforeStepHooks(ctx, step, err)
, it happens only for "root-level" steps.
And here are multisteps are being invoked:

godog/suite.go

Line 354 in b2672bb

} else if ctx, err = s.maybeSubSteps(def.Run(ctx)); err != nil {
.

I think implementation strategy could be to change def.Run(ctx) into a suite method that would receive def and ctx and wrap a call with hooks.
Maybe we can just use (s *suite) runStep for that (not sure if there are any caveats or implications).

wichert added a commit that referenced this issue Aug 5, 2022
This demonstrates the bug reported in #486
wichert added a commit that referenced this issue Aug 17, 2022
This demonstrates the bug reported in #486
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants