0
votes

I´m trying to do test in grails 2 with Coverage, but I cannot acces to method:

It said that: "java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request..."

class UserController implements  ResourceLoaderAware{

def index(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    respond User.list(params), model:[userInstanceCount: User.count()]
}

}

import grails.test.mixin.Mock
    import grails.test.mixin.TestMixin
    import grails.test.mixin.support.GrailsUnitTestMixin
    import spock.lang.Specification
    import com.snt.olucarodashboard.UserController
    import grails.test.mixin.TestFor


    @TestMixin(GrailsUnitTestMixin)

    class UserControllerSpec extends Specification {

        UserController controller=new UserController()
        void "Test the index action returns the correct model"() {
            when: "The index action is executed"
            controller.index(0)

        }
    }

What is the problem?

Thanks

1

1 Answers

0
votes

Instead of instantiating the controller yourself, use the @TestFor annotation at the class level

import grails.test.mixin.TestFor

@TestMixin(GrailsUnitTestMixin)
@TestFor( UserController )
class UserControllerSpec extends Specification {

    void "Test the index action returns the correct model"() {
        when: "The index action is executed"
            controller.index(0)
    }

}