0

I have a service method that takes params as an input and creates a domain class passing in params for auto binding. Testing of this is simple by specifying params as a map of key value pairs. However, I don't know how to simulate an array of associated objects in the params map.

I observed client side sending in params in the following format:

["description":"abc",  "subTask[0].name": "first subtask name"]

How do I mock this type of params? Since this is not a controller test I can not use mockParams AFAIK.

4

2 回答 2

1

The answer turns out to be this: Since controller params map parsing is not available in a Service class you have to convert values and objects on your own. So in my example I have to manually create an array of associated objects within the params map. Here is an example:

  def params = [ state: "Open", type: "Cable", 
            needByDate: new Date("Fri May 11 00:00:00 PDT 2012"),
            //"subTasks[0]":["name":"ABC"]  //no workie
            "subTasks": [new Task("name": "ABC")]
  ]
  mockDomain(Task.class)
  Task task= service.saveNewRequest(params)

Hope this helps somebody.

于 2012-05-31T23:36:32.803 回答
0

The params map in Grails does magic stuff with parameter names that contain dots, making them available in the controller as "sub-maps". For example if you have parameters a.b=1 and a.c=2 you can ask for params.a and get a map [b:1, c:2]. So try something like this:

["description":"abc",  "subTask[0]":["name": "first subtask name"]]
于 2012-05-31T23:08:33.290 回答