Testing my controller with Spock

I need to test a controller action index

(generated by grails command generate-all

). I have a test like this (in Spock):

package mnm.schedule

import grails.test.mixin.*
import org.junit.*
import grails.plugin.spock.*
import spock.lang.Specification
import org.example.*;

class UserControllerSpec extends ControllerSpec {


def "test"() {
            setup:
            mockLogging(UserController, true)

            when:
            controller.index()

            then:
            redirectArgs.action == "list"
    }

} 

      

I am getting an error like this:

Error Error running script test-app :spock : cannot find shared instance field (Use --stacktrace to see the full trace)

      

After some time I can run the test, the test gets PASSED.

What's really going wrong? Why is it showing this error the first time? I am new to the Spock environment.

Thanks in advance.

+3


source to share


1 answer


Given that you are using Grails 2.xx, you should use the @TestFor annotation, which extends the unit test classes with mixins.



In your case, you must add @TestFor(UserController)

as class-level annotations so that you can use the method mockLogging

.

+3


source







All Articles