I have a put request that I am trying to unit test for creating a user object.
Unit Test:
void testPUTXMLResponse() {
def mockUser = new User(username:"fred", password:User.encrypt("letmein"), firstName:"Fred", lastName:"Flintstone", middleName:"T", phone:"555-555-5555", email:'[email protected]', activationDate:new Date(), logonFailureCount:0)
mockDomain(User, [mockUser])
def mockUserXML = mockUser as XML
mockRequest.method = 'PUT'
mockRequest.contentType = 'text/xml'
mockRequest.format = 'xml'
mockRequest.content = mockUserXML.toString().getBytes()
controller.create()
def updatedUser = XML.parse(mockResponse.contentAsString)
assert updatedUser.id == 1
}
Controller Action:
def create = {
println request.xml
def user = new User(params.user)
if(!user.hasErrors() && user.save()) {
println user.id
withFormat {
html { /*render(view:"show", [user:user])*/ }
xml { render user as XML }
json { render user as JSON }
}
} else {
println user.errors
withFormat {
html { /*render(view:"create", [user:user])*/ }
xml { render user.errors as XML }
json { render user.errors as JSON }
}
}
}
For some reason the params map is null when the controller action is executed. I have a very similar unit test for POST requests and it works just fine.