liuyingyyll / spock

Automatically exported from code.google.com/p/spock
0 stars 0 forks source link

interactions in given/setup blocks should have precedence over those in setup methods #199

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Describe the enhancement you have in mind.

interactions in given/setup blocks should have precedence over those in setup 
methods as discussed in: 
http://groups.google.com/group/spockframework/browse_thread/thread/3e20f7466dd62
48b

Is it possible to override the behaviour (return value) for one
specific test method: see example below (also available at:
http://meetspock.appspot.com/script/36001)
"testing exceptional behaviour" fails where I want it to succeed

interface MyInterface {
    int myMethod()

}

class MyInterfaceTest extends Specification {
    def myInterface = Mock(MyInterface)

    def setup() {
        myInterface.myMethod() >> 1
    }

    def "testing regular behaviour"() {
        when:
         def result = myInterface.myMethod()
        then:
         result == 1
     }

    def "testing exceptional behaviour"() {
        given:
         myInterface.myMethod() >> 2
        when:
         def result = myInterface.myMethod()
        then:
         result == 2
      } 
}

Which particular problem would this enhancement solve for you?
it would ease the setup of testing expetional behaviour, where for all other 
tests use the same 'normal' behaviour of the mock, so you don't have to setup 
this for each test.

Please provide any additional information below. You can also assign
labels.

Original issue reported on code.google.com by frederic...@gmail.com on 13 Sep 2011 at 11:08

GoogleCodeExporter commented 8 years ago
It would make more sense to me to override the interactions defined in the 
setup method within the setup/given block

Original comment by Alessand...@gmail.com on 20 Nov 2012 at 4:52

GoogleCodeExporter commented 8 years ago
I completely agree with this change. Just like in junit any changes I make to 
mocks or global variables are overridden in an individual test. I found it very 
confusing when I would change the behavior in an individual tests setup block 
to find out it is ignored. The specification setup should be to setup the base 
line for all tests. Then each individual test should be able to change behavior 
as it sees fit.

Original comment by Chef098 on 5 Jul 2013 at 2:35

GoogleCodeExporter commented 8 years ago
+1 This change also makes easier to migrate from testng or junit to Spock, as 
it is the default behaviour on these frameworks.

Original comment by fsa...@gmail.com on 16 Jul 2015 at 5:35