0
編輯2:找出如何傳遞一個整數,但是現在我的測試失敗了,因爲返回的值是空的,但渲染是網頁上的正確值。Spock在Grails中測試POST服務
編輯:我想通過POST使用request.method如何傳遞值,但是我現在無法弄清楚如何使用請求傳遞一個int到控制器中,因爲每個addParameter都需要一個字符串。
我有一個在grails中的轉換應用程序,其中二進制和十六進制服務在將其轉換爲二進制或十六進制並返回結果之前通過後置參數。我現在正在編寫一些Spock測試來測試每個服務,但是如果參數需要通過POST而不是GET來發送,我似乎無法弄清楚如何測試服務。
二進制轉換服務:
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)
}
}
斯波克測試:
void "Binary Service should return 1100"() {
given:
def convert = new ConvertController()
when:
def result = convert.binary(12)
then:
result == 1100
}
這是我的測試,現在返回:
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)