{"id":13088,"date":"2014-04-21T15:56:47","date_gmt":"2014-04-21T10:26:47","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=13088"},"modified":"2015-10-16T15:31:32","modified_gmt":"2015-10-16T10:01:32","slug":"unit-testing-parameters-in-forward-method","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/unit-testing-parameters-in-forward-method\/","title":{"rendered":"Unit testing parameters in forward method"},"content":{"rendered":"<p>I always wanted to test the parameters being passed in the forward method in a controller in my unit tests.<br \/>\nSuppose we have any controller like:<\/p>\n<p>[java]<br \/>\nclass DemoController {<br \/>\ndef anyAction() {<br \/>\n forward(controller: &#8220;demo&#8221;, action: &#8220;test&#8221;, params: [param1: &#8220;1&#8221;])<br \/>\n}<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p>to test this forward action, we first need to mock the forward method.<\/p>\n<p>To do so, all we require is:<\/p>\n<p>[java]<br \/>\ndef fwdArgs<br \/>\ncontroller.metaClass.forward = { Map map -><br \/>\n fwdArgs = map<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>to be added in the setup.<\/p>\n<p>To test the params, all we need to do is:<\/p>\n<p>[java]<br \/>\nfwdArgs.params.param1<br \/>\nfwdArgs.controller==&#8217;demo&#8217;<br \/>\nfwdArgs.action==&#8217;test&#8217;<br \/>\n[\/java]<\/p>\n<p>Using this we would be able to test the params being passed in the forward method.<\/p>\n<p>Here is the complete test case :<\/p>\n<p>[java]<br \/>\n@TestFor(DemoController)<br \/>\nclass DemoControllerSpec extends Specification {<\/p>\n<p>void &#8220;test anyAction&#8221;() {<br \/>\n setup:<br \/>\n def fwdArgs<br \/>\n controller.metaClass.forward = { Map map -><br \/>\n fwdArgs = map<br \/>\n }<\/p>\n<p>when:<br \/>\n controller.anyAction()<\/p>\n<p>then:<br \/>\n fwdArgs.params.param1<br \/>\n fwdArgs.controller==&#8217;demo&#8217;<br \/>\n fwdArgs.action==&#8217;test&#8217;<\/p>\n<p>}<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I always wanted to test the parameters being passed in the forward method in a controller in my unit tests. Suppose we have any controller like: [java] class DemoController { def anyAction() { forward(controller: &#8220;demo&#8221;, action: &#8220;test&#8221;, params: [param1: &#8220;1&#8221;]) } } [\/java] to test this forward action, we first need to mock the forward [&hellip;]<\/p>\n","protected":false},"author":115,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[7],"tags":[1399,4840,1159],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/13088"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/115"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=13088"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/13088\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=13088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=13088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=13088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}