Products

Products
Viewing By Entry / Main
May 20, 2009

CFC Method Testing in Coldbox

I got tired of trying to get cfUnit working and all I wanted to do was test component methods. I wanted to be able to simply point to it, test it, and enter arguments if required. And I wanted this set up somewhere easy I could use quickly, repeatedly, effortlessly, and painlessly (word?).

In almost all my applications I have a 'webmaster' directory where I run test. Lately I've found myself getting fancy with setting up a web development control panel of sorts. Whereas web sites generally have administration control for admin users, I've been building in webmaster control for developers. Mostly things like server and framework info, links to documentation, framework and site functionality examples, and etc.

Now I'm adding a little CFC Test form where I enter my cfc path, the method name, and arguments. It will pass the event argument so you can also test cfc handlers.

Warning, I just created this a few hours ago. It's tested to work but I haven't cleaned it up or put in validation and error handling yet.

views/webmaster/index.cfm (webmaster control panel)

<cfform name="cfctestform" action="#cgi.SCRIPT_NAME#" method="post" format="html">
   <cfinput name="do" type="hidden" value="webmaster.panelcontrol.testmethod" />
   <cfinput name="sbIsEnabled" type="hidden" value="0" />
   Enter Component Path:<br /><cfinput name="cfcpath" type="text" />
   <br />
   Enter Method to Test:<br /><cfinput name="cfcmethod" type="text" />
   <br />
   Arguments:(comma seperated list)<br /><cfinput name="methodargs" type="text" width="80" />
   <br />
   <cfinput type="submit" name="submit" value="Run Test" />
</cfform>

events/webmaster/panelcontrol.cfc (Coldbox handler)

<cffunction name="testmethod" access="public" returntype="void" output="true">
   <cfargument name="Event" type="coldbox.system.beans.requestContext">
   <cfset var rc       = arguments.event.getCollection()>
   <cfset var ret       = "">
   <cfset var cfcObj    = createObject("component",rc.cfcpath)>
   <cfif listLen(rc.methodargs)>
      <cfloop from="1" to="#listLen(rc.methodargs)#" index="i" step="2">
         <cfset evaluate("arguments['#listGetAt(rc.methodargs,i)#'] = '#listGetAt(rc.methodargs,i+1)#'")>
      </cfloop>
   </cfif>
   <cfinvoke component="#rc.cfcpath#" method="#rc.cfcmethod#" argumentcollection="#arguments#" returnvariable="ret">
   <cfscript>
      //Display       event.setValue("cfctestresults",ret);
      event.setValue("author","Clint Willard");
      event.setView("webmaster/dspCfcTestResult");
   </cfscript>
</cffunction>
Basically it loops through the method arguments list you entered in the form setting them as part of the arguments struct. That arguments struct is then passed to the invoked component and method, along with the event object. Then the view is set to output with the test results, whatever the method is set to return.

views/webmaster/dspcfcTestResult.cfm

<cfscript>
   cfcresult = event.getValue("cfctestresults","NA");
</cfscript>
<cfoutput>
   #dump(cfcresult)#
</cfoutput>
KISS output.

Let me know if this might be a good idea to expand on or should I just LOL learn cfUnit? And the webmaster information and control panel stuff?

Comments

There are no comments for this entry.


Copyright © 2005-2006 Clint Willard. All rights reserved.
Aura skin for Clint Willard's BlogCFC inspired by Brooks Bilson's Bolg.
All trademarks property of their owners.