EDIT 2: Figured out how to pass an integer, however my now my test is failing because the value coming back is empty but the render is the correct value on the web page.
EDIT: I've figured out how to pass values by POST using the request.method, however i now can't figure out how to pass an int into the controller using request, as each addParameter expects a string.
I have a convert application in grails, where both the binary and hex service take a parameter via post before converting it to either binary or hex and returning the result. I'm now writing some Spock tests to test each service however i can't seem to figure out how to test the service if the parameter is required to be sent via POST rather than GET.
Binary Convert Service:
def binary() {
if (request.method == 'POST') {
if (session.user) {
Integer number = params.getInt('number')
String binary = Integer.toBinaryString(number)
def r = new Results()
r.customerID = session["id"]
r.username = session["username"]
r.ConvertService = "binary"
r.number = number
r.result = binary
r.date = new Date()
r.time = new Timestamp(System.currentTimeMillis())
if(r.save()) {
render binary
} else {
render r.getErrors()
}
} else {
redirect(controller: 'main')
}
} else {
response.sendError(405)
}
}
Spock test:
void "Binary Service should return 1100"() {
given:
def convert = new ConvertController()
when:
def result = convert.binary(12)
then:
result == 1100
}
This is what my test returns right now:
groovy.lang.MissingMethodException: No signature of method: eadassignment.ConvertController.binary() is applicable for argument types: (java.lang.Integer) values: [12]
Possible solutions: binary(), any(), <init>(), index(), every(), find()
at org.codehaus.groovy.grails.plugins.web.api.ControllerTagLibraryApi.methodMissing(ControllerTagLibraryApi.java:97)
at eadassignment.ConvertControllerSpec.Binary Service should return 1100(ConvertControllerSpec.groovy:22)
Figured out all my problems. Last one was because the function checked if a valid user existed which the test was failing for obvious reasons.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments