jbwheatley / pact4s

Apache License 2.0
29 stars 11 forks source link

Be able to define a "before each" / "after each" method for provider state #368

Closed gaeljw closed 1 year ago

gaeljw commented 1 year ago

Unless I missing something, there's no "easy" way to define a beforeEach/afterEach logic to run automatically before or after each contract request verification on the provider side.

Take for instance the following code sample:

class PactVerificationsTest extends PactVerifier {

  override def provider: ProviderInfoBuilder = ProviderInfoBuilder(...)
    ...
    .withStateChangeFunction { 
      case ProviderState("State A", params) =>
         resetState()
         // ... some specific state logic
      case ProviderState("State B", params) =>
         resetState()
         // ... some specific state logic
    }

   ...

}

We have to put the resetState() call in every state.

Would it make sense to have something like a .withStateChangeBeforeHook { f() } that would make the verifier automatically call f() before each request verification?

If you think it make sense, I'll be happy to help implement it.

jbwheatley commented 1 year ago

Yeah I think this sounds like a nice thing to have! If you have time, please go ahead and open a PR 😋

jbwheatley commented 1 year ago

Closing as the beforeEach was implemented in #378, and as mentioned on that PR, adding an after hook would involve lifting a lot of code out of the ProviderVerifier class from pact-jvm to ensure that the after hook runs after each interaction has been run. This is not something I would like to do as it makes it easier for pact4s to fall out of step with pact-jvm