1
斯波克測試動作
我有我想測試時,內容類型的動作是application/json
在Grails的
我的行動看起來是這樣的:
def save() {
request.withFormat {
json {
def colorInstance = new Color(params.colors)
render "${colorInstance.name}"
}
html {
//do html stuff
}
}
我有以下,但它似乎並沒有工作:
def "js test"() {
when:
controller.save()
request.contentType = "application/json"
request.content = '{"colors": {"name": "red"} }'
then:
response.contentAsString == "red"
}
我相信問題是我在我的測試中發送json到控制器的方式。這是正確的方法嗎?
錯誤是:
response.contentAsString == "red"
| | |
| null false
如果我稍微修改控制器是:
json {
def colorInstance = new Color(params.colors)
render "${params.colors}"
}
隨後還錯誤是一樣的:
response.contentAsString == "red"
| | |
| null false
所以我懷疑params.colors
是從未到達控制器......?
JS是Javascript,使用 'JSON' 內withFormat代碼來處理JSON。 – dmahapatro
我已經改變了,但仍然是相同的錯誤。我認爲json沒有被傳遞給控制器。 – Anthony
在測試中請求設置'content-type'之前,您正在調用該操作。 – dmahapatro