Multiple when/then with where for each other
Asked Answered
E

1

9

I'm trying to write functional test with Spock and Geb. I want to use in one test method multiple blocks of when/then with where for each block. Is it possible? Or I should use one where for all the when/then?

Elsy answered 21/11, 2013 at 9:21 Comment(4)
What is your experience on each approach? Do you face any problem using either of them?Sinatra
@dmahapatro, I'm trying to simulate user's behavior with test: click to edit button, fill fields, click save and etc. I'm newbie in spock and functional. I do something wrong? Thanks, dmahapatroElsy
A common way to test a workflow is to have a separate method for each step. Then you can have one when-then-where in each method. To use this style of testing, annotate the test class with @Stepwise.Ayana
@PeterNiederwieser, thanks, I will rework my test with this style.Elsy
S
14

You will not be able to use where per interaction, it would complain about the usage. You would end up with a single where for multiple interactions. Follow this as a sample:

def test() {
  given:   
  def c
  def d

  when:
  c = a + b

  then:
  c == result

  when:
  d = e - f

  then:
  d == res

  where:
  a | b |result | e |f |res
  1 | 2 | 3     | 7 |5 |2
}
Sinatra answered 21/11, 2013 at 9:43 Comment(4)
If you have multiple interactions to test, then yes you can very well do that in one method. Follow docs for details. @baxxabitSinatra
Thank you! One small question. In an example you use one where block for every when/then. If I define variable like 'name' in where, it will be available in every when block?Elsy
Please, answer on my second comment :)Elsy
I am not sure which example you are referring to. But yes from the sample mentioned in the answer above, you can refer a variable inside data grid of where block like 'name' in every when block. @baxxabitSinatra

© 2022 - 2024 — McMap. All rights reserved.