Grails IntegrationSpec and multiple calls to Controller
Asked Answered
R

0

6

I have a test where I need to make several calls back to back to test a controller using Grail's IntegrationSpec.

    when: "User starts a flow"
    controller.userSessionService.user = user
    controller.request.method = "POST"
    controller.request.contentType = "text/json"
    controller.request.content = ([lId: l.id] as JSON).toString().getBytes()

    controller.start()

    then:
    ...

    when: "User does next step"
    controller.userSEssionService.user = user
    controller.request.method = "POST"
    controller.request.contentType = "text/json"
    controller.request.content = ([data1: data] as JSON).toString().getBytes()

    controller.nextStep()

    then:
    ...

So forth and so on. The problem is trying to change the request's content for the 2nd call and it won't work. It seems to keep only the first call to request.content. I know in prior versions there were helper methods on GroovyTestCase, but since I'm using Specs there aren't any methods I can call to reset the request/response for a controller. I've tried to re-instantiate the controller before each when: sections using given:. But the test just crashed.

I have to make these calls in this order and test the state changes between each call. I can't break these up across several method calls. I'm not sure this matters in this case, but I'm using MongoDB.

How do I make multiple calls to the controller and have the request/response reset between each call when using Spock IntegrationSpec?

Rostov answered 14/8, 2014 at 3:35 Comment(2)
So I suppose since Grails wants a Controller instance per request/response cycle then if you did several request/response you'd need to instantiate the controller multiple times. However, because Grails has so much "magic" happening you can't easily do that. If I was instantiating things then it'd be easier. Or if magic was better documented same thing.Rostov
have you tried to write functional tests? that works better for controller end to end testing than an integration testsBiform

© 2022 - 2024 — McMap. All rights reserved.