With the first stages of the project completed and all parts in working order, the application should be tested. In the case of our ScriptRepository.as file, some built-in methods of the application and the objects used in the application make our life easier. We have defined toString( ) methods of the two main objects, and also a test( ) method to make sure that the object coming back from remote services has been cast to the correct datatype.
To use the toString( ) method, simply sprinkle the code with the following, substituting the object name:
trace(myObject.toString( ));
This can even be used in the init( ) method of your constructor to make sure your objects are being instantiated properly and at the right times:
trace(this.toString( ));
The test( ) method is used in the responder onResult( ) method. Simply call the test( ) method on the result coming from the remote service. If the result has been cast to the correct object type, the test( ) method will fire. If it hasn't been cast properly, the method will not fire:
ScriptObject.prototype.onResult = function (result) { result.test( ); // more code... }
During testing we also set up a special server-side logging method to log each object that was being created on the server. The server-side code is shown in Example 14-16 and shows how easy it is to create a custom log file for a custom object.
<cffunction access="remote" name="objToString" output="false" returntype="string" > <cfargument type="any" name="obj" /> <cfloop collection = #obj# item = "i"> <cfset temp = '#temp##i#: #obj.get(i)##chr(13)##chr(10)#'> </cfloop> <cfreturn temp /> </cffunction> <cffunction name="logScript" access="remote" returntype="any"> <cfargument name="scriptobj" type="any" /> <cffile action="append" file="c:\log.txt" output=#this.objToString (ScriptObj)#> </cffunction>
This method is called directly from another server-side service with a simple line, passing an object to the logScript( ) method:
<cfset temp = logScript(scriptObj)>
Additionally, this logging routine will work with any flashgateway.IO.ASObject object.
The server-side services all contain try/catch blocks around the portions of the method that may throw an error. During debugging, you may want to comment out the try/catch blocks (using <!--- and ---> comment delimiters, as shown in bold) so that your errors are not captured; that way you can see the original error message as the server generated it:
<!--- <cftry> ---> <cfquery name="rsScripts" datasource="ScriptRepository"> SELECT * FROM Scripts WHERE ScriptID = <cfqueryparam cfsqltype="cf_sql_numeric" value="#ScriptID#"> </cfquery> <!--- <cfcatch type="any"> <cfthrow message="There was a database error" /> </cfcatch> </cftry> --->
Another good source of information while debugging can be found in the flash.log file created in the ColdFusion_root\logs directory.