How do I check the order of method calls and redirects when testing blocks in a graph?

I am seeing some strange behavior with the grails controller methods which caused me to create a test case to see if I can consistently reproduce it. So, I will explain the question first in order to better understand my question.

I've had some weird behavior lately: when the grails method is called, it sometimes doesn't finish the entire method implementation (basically all code goes to server calls) and redirects too early. I can see that my service methods are still working, but the ui is already (sometimes also wrong) redirected incorrectly. I've tried debugging it, but that also brings me to the same places. I will be in the service method but see the page already rendering to the ui. If I could get a good test case I believe it might be easier to debug.

Example controller code:

def blam() {
   Foo foo = Foo.get(params.id)
   String msg = service.method1(foo)
   service.method2(foo)
   service.doSomething(foo)
   redirect(action:"list", controller: "blam", params: [msg:msg, *:params])
}

      

Sample Test Code: The problem below is that it doesn't actually check the order of the calls, I want to check that it is calling the methods in the correct order. I found a couple of examples that say you can call multiple declarations, but I can't see the difference if I move the methods around and rerun the test.

def "test service call order and redirect"(){
   when:
   controller.actionMethod()

   then:
   service.method1(_) * 1
   service.method2(_) * 1
   service.doSomething(_) * 1

   then:
   response.redirectedUrl == "bam/list"   
}

      

Any help on how to test this scenario would be greatly appreciated! Also I know this is not the main question, but ideas about redirecting are welcome too! (or should I post another question?)

Hooray!

+3


source to share


1 answer


Use multiple blocks then

:

then:
   service.method1(_) * 1

then:
   service.method2(_) * 1

then:
   service.doSomething(_) * 1

      



Then the order should be followed (see http://spock-framework.readthedocs.org/en/latest/interaction_based_testing.html#invocation-order )

+5


source







All Articles