groovy - Testing action with spock in Grails -
i have action test when content-type application/json
my action looks this:
def save () { request.withformat { json { def colorinstance = new color(params.colors) render "${colorinstance.name}" } html { //do html stuff } }
i've got following doesn't seem work:
def "js test" () { when: controller.save() request.contenttype = "application/json" request.content = '{"colors": {"name": "red"} }' then: response.contentasstring == "red" }
i believe problem in way i'm sending json controller in test. correct way?
error is:
response.contentasstring == "red" | | | | null false
if modify controller be:
json { def colorinstance = new color(params.colors) render "${params.colors}" }
then error same:
response.contentasstring == "red" | | | | null false
so suspect params.colors
never reached controller...?
this works me:
note:- used given
set parameters. looks setting json
request binds params
in controller.
def save() { request.withformat { json { def colorinstance = new color(params.colors) render "${colorinstance.colorname}" } html { //do html stuff } } }
//domain color
class color { string colorname boolean isprimarycolor }
//spock test
def "json test" () { given: request.contenttype = "application/json" request.json = '{colors: {colorname: "red", isprimarycolor: true} }' when: controller.save() then: assert response.status == 200 assert response.contentasstring == "red" }
Comments
Post a Comment