Mixing interactions and conditions while using Spy

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Mixing interactions and conditions while using Spy

Serban Iordache
Hi,

Please take a look at the following code. It has 3 tests, which differ only in their then: blocks. The first test checks a condition, the second one an interaction, and the third one both the interaction and the condition. The third test fails.

import spock.lang.Specification

class SpyMix extends Specification {
 
static class Square {
   
double getLength() {32}

   
public double getArea() {
     
return getLength() * getLength();
   
}
 
}

 
def "check condition with stubbed length"() {
   
def square = Spy(Square) { getLength() >> 10 }
   
when:
   
def area = square.area
   
then:
    area
== 100
  }

 
def "check interaction with stubbed length"() {
   
def square = Spy(Square) { getLength() >> 10 }
   
when:
   
def area = square.area
   
then:
   
2 * square.getLength()
 
}

 
def "check interaction and condition with stubbed length"() {
   
def square = Spy(Square) { getLength() >> 10 }
   
when:
   
def area = square.area
   
then:
   
2 * square.getLength()
    area
== 100
  }
}


The third test fails with the following error message:

Condition not satisfied:

area == 100
|    |
|    false
1024.0



Is this a bug or the expected behavior?

Thanks!

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Mixing interactions and conditions while using Spy

Serban Iordache
I should have read the reference documentation more carefully. The section "Combining Mocking and Stubbing" explains this behavior and states that "Mocking and stubbing of the same method call has to happen in the same interaction".
The third test could be implemented like this:

  def "check interaction and condition with stubbed length"() {
   def square = Spy(Square)
   when:
   def area = square.area
   then:
   2 * square.getLength() >> 10
   area == 100
 }



--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.
Loading...