Question

I'm new in create unit test in grails so this question may seem silly.

I have create a unit test of a method that receives input data from a form, how can I emulate the call of the form to the controller in the unit test?

this is my method:

def createNewUser(UserSec user) {
          def user = new User()
          user.name= params.name
          user.surname = params.surname
          user.save(flus:true)
}

In this case, name and surname come in the form of parameters from the form, how can I get them from unit tests?

Thanks to all

Bye!

Was it helpful?

Solution

Take a look at the documentation on testing within Grails. It covers specifically how to set parameters for testing controllers. From the examples:

import grails.test.mixin.TestFor
import spock.lang.Specification
@TestFor(PersonController)
class PersonControllerSpec extends Specification {

    void 'test list'() {
        when:
        params.sort = 'name'
        params.max = 20
        params.offset = 0
        controller.list()

        then:
        // …
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top