我在nabble here
上提出了完全相同的问题我正在尝试在控制器集成测试中发送params或不同的域。 但是无法使用“book”前缀绑定到域类
//正在测试的控制器操作
def saveBook = {
def book = new Book()
bindData(book, params["book"], [include: ['publicPrivacy', 'description', 'title'])
}
//集成测试 -
def bookController = BookContoller()
//Doesn't Bind
bookController.params.publicPrivacy = false
bookController.params.description = "Best book in the world"
bookController.params.title = "The world"
bookController.params.book.publicPrivacy = false
bookController.params.book.description = "Best book in the world"
bookController.params.book.title = "The world"
bookController.params["book"].publicPrivacy = false
bookController.params.[book.description] = "Best book in the world"
bookController.saveBook()
如何设置带有前缀的“params”以发送到控制器,以便它们绑定到域?
答案 0 :(得分:4)
对于params命名空间工作,我必须使用org.codehaus.groovy.grails.web.servlet.mvc.GrailsParameterMap
作为params对象。例如:
def p = ['book.description': "Best book in the world", ...]
def request = [getParameterMap: { -> p }] as javax.servlet.http.HttpServletRequest
controller.params = new org.codehaus.groovy.grails.web.servlet.mvc.GrailsParameterMap(request)
controller.saveBook()