Unit testing a controller method? -


I have a controlling method such as:

  def search = {dig query = Parameter. Submitting result as ... JSON}  

How do I test this? Specifically, how do I call search to set up params.query and how do I check the results of the method render ? Is the duplicate method of render method, maybe?

itemprop = "text">

Subclass Grails for your unit tests automatically instantiate your controller and duplicate versions and render out That is to allow you to easily test the results, just assign test inputs to the controller. To set up the ultimate test.

Example:

  class SomethingController {def search = {df query = params.query ... content ... render results as JSON}}  

test looks like this:

  class SomethingControllerTests grails.test.ControllerUnitTestCase extended {zero test search () {controller.update.on = "test query" controller .seert () assertEquals "expected results", controller.response.contentAsString}} Note: You can also use ControllerUnitTestCase for integration testing, if you have a database with A complete integration environment is required.