How to call a method or close in a where clause in a spock test

I work with Spock certificates and I wonder if something like this is possible. My approaches do not work, and I wonder if any of you had similar intentions and found a way.

I want to call a method or closure that needs to be called only for each corresponding where-clause in order to configure some things. I can’t just call all of these methods, as this will ruin my test. The only way I've found so far is to check what the current state is and call the method appropriately in the if statement: if (state == SomeStateEnum.FIRST_STATE) {somePrivateMethodFromSpec ()}, but I'm wondering if it failed make at its best. I hope my intentions are clear (sorry, I'm not a native speaker). Below is an example of code that might be a little better to understand what I want to do. Thank you in advance.

def 'is this even possible?'() {
    when:
    def resultState = service.someServiceMethod(param)

    then:
    resultState == state

    where:
    state                       | param     | method
    SomeStateEnum.FIRST_STATE   | 'param1'  | somePrivateMethodFromSpec()
    SomeStateEnum.SECOND_STATE  | 'param2'  | someOtherPrivateMethodFromSpec()
}

private def somePrivateMethodFromSpec() {
    someServiceMock.demand.AAA() {}
}

private def someOtherPrivateMethodFromSpec() {
    someServiceMock.demand.BBB() {}
}

def 'or maybe something like this?'() {
    when:
    closure.call()
    def resultState = service.someServiceMethod(param)

    then:
    resultState == state

    where:
    state                       | param     | closure
    SomeStateEnum.FIRST_STATE   | 'param1'  | {println '1'}
    SomeStateEnum.SECOND_STATE  | 'param2'  | {println '2'}
}

Decision:

def 'this will work'() {
    "$someOtherPrivateMethodFromSpec"()
    "$somePrivateMethodFromSpec"()
    def resultState = service.someServiceMethod(param)

    then:
    resultState == state

    where:
    state                       | param     | method
    SomeStateEnum.FIRST_STATE   | 'param1'  | "somePrivateMethodFromSpec"
    SomeStateEnum.SECOND_STATE  | 'param2'  | "someOtherPrivateMethodFromSpec"
}

private def somePrivateMethodFromSpec() {
    someServiceMock.demand.AAA() {}
}

private def someOtherPrivateMethodFromSpec() {
    someServiceMock.demand.BBB() {}
}
+4
source share
2 answers

, , , :

class MyFirstSpec extends Specification {
  def "let try this!"() {
    expect:
      "${method}"() == method
    where:
      method << ["method1", "method2"]
  }

  private String method1(){
     return "method1"
  }

  private String method2(){
     return "method2"
  }
}

+5

, where.

. ({println ( "1" )})

.

def 'or maybe something like this?'() {
    when:
    closure.call()
    def resultState = service.someServiceMethod(param)

    then:
    resultState == state

    where:
    state                       | param     | closure
    SomeStateEnum.FIRST_STATE   | 'param1'  | ({println '1'}) 
    SomeStateEnum.SECOND_STATE  | 'param2'  | ({println '2'})
}
+3

All Articles