Simple ColdBox Model Unit Tester

I just started using ColdBox and wanted to use the unit testing features but it wasn't working with MXUnit so I developed the following set of conventions and components to help me test my models.

For each model that you want to test create a component with the name test in front of it. For example the a user model you would create testuser. In that component create methods that will be responsible for testing each of the model methods. You could choose otherwise but I find this the most intuitive.

Place the testModel component in your controller folder. In the case of ColdBox that is handlers. Call the testModel handler, with the name of the model that you want to test, as the action and it will give you a list of all of the test methods available.

Each method in the testModel method should return the result or array of results in the prc.results variable. This will be dumped using cfdump by the view.

With a little tweaking I know that this could be adapted to FW1 or probably most any other framework.

I believe that this is a simple straight forward way of testing your models if you want a more robust system then go checkout MXUnit

handler/testModel.cfc

component{
	function onMissingAction(event,rc,prc,missingAction,eventArguments){
		//var rc = event.getCollection();		
		/* path: index.cfm/testModel/model?method=method_name */
		prc.action = missingAction;
		prc.model = getModel( "test#missingAction#");
		if( event.valueExists("method")) {
			prc.results = evaluate("prc.model.#rc.method#()");
		} else {
			var tmp = GetComponentMetaData("model.#missingAction#");
			prc.methods = ArrayNew(1);
			for( var i=1; i lt ArrayLen(tmp.functions); i++ ) {
				ArrayAppend( prc.methods, tmp.functions[i].name );
			}			 
		}
		event.setView('test/model');
	}
}

model/testMODEL_NAME.cfc

component cache="false" {
	property name="MODEL_NAME" inject="MODEL_NAME";
	
	testMODEL_NAME function init(){
		return this;
	}

	public any function METHOD_NAME(  ) {
		try{
			return MODEL_NAME.METHOD_NAME('users','username','username','a');
		} catch( any e) {
			Throw ( message="An error occured in MODEL_NAME.METHOD_NAME: #e.message#" );
		}
	}
}

view/model.cfm

<h2>Unit Testing for Model: <cfoutput>#prc.action#</cfoutput></h2>
<hr>
<cfif isDefined("prc.methods") >
<h2>Methods:</h2>
<ul>
<cfloop array="#prc.methods#" index="item"><cfoutput>
<li><a class="brand" href="#event.buildLink('#rc.event#?method=#item#')#"><strong>#item#</a></li> 
</cfoutput></cfloop>
</ul>
</cfif>
<cfif isDefined( "prc.results") >
<h2>Results:</h2>
<cfdump var="#prc.results#" expand="no">
</cfif>