groovy - How to partially mock service in Grails integration test -


i attempting write test controller calls service method. mock dependent method within service.

my spec follows:

mycontroller mycontroller = new mycontroller() def mockmyservice  def "my spy should called"() {     when:         mockmyservice = spy(myservice) {             methodtospy() >> {                 println "methodtospy called"             } // stub out content of fn         }         mycontroller.myservice = mockmyservice         mycontroller.callservice()      then:         1 * mockmyservice.methodtospy() } 

when attempt run test, following error:

 failure:  | spy should called(spybug.mycontrollerspec)  | few invocations for: 1 * mockmyservice.methodtospy()   (0 invocations) unmatched invocations (ordered similarity): 1 * mockmyservice.servicemethod() 1 * mockmyservice.invokemethod('methodtospy', []) 1 * mockmyservice.invokemethod('println', ['in servicemethod call methodtospy']) 1 * mockmyservice.invokemethod('println', ['back methodtospy']) 

as can see, spock capturing groovy invokemethod call, not subsequent call actual method. why happening?

the complete project available here.

try this:

def "my spy should called"() {     given:     mockmyservice = mock(myservice)     mycontroller.myservice = mockmyservice      when:     mycontroller.callservice()      then:     1 * mockmyservice.methodtospy(_) >> { println "methodtospy called" }    } 

according spock documentation stubs, if want use cardinality, must use mock , not stub.

http://spockframework.github.io/spock/docs/1.0/interaction_based_testing.html#_stubbing


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -