problem with Handler Testing (using BaseHandlerTest.cfc)

Does anyone have a somewhat more complete example of "Targeted Handler
Testing" beyond the "Basic Setup" shown at
http://wiki.coldbox.org/wiki/WhatsNew:3.0.0.cfm#New_Targeted_Handler_Testing?

I generated a test application using the Platform Utilities (Simple Template).
I verified that the generated integration test of the General handler worked.
I added a "Targeted Handler Test" (TargetedGeneraTest.cfc):

/**
* @handler Sample.handlers.General
* UDFLibraryFile ""
*/
component extends="coldbox.system.testing.BaseHandlerTest" {
  function testindex(){
    var rc = mockRequestContext.getCollection();
    var prc = mockRequestContext.getCollection(private=true);
    handler.index(mockRequestContext,rc,prc);
    debug(rc);
    assertEquals("Welcome to ColdBox!", rc.welcomeMessage, "Failed to
assert welcome message");
  }
}

This works.

I added an event to my test application's General handler that invokes a model:
  <cffunction name="add" returntype="void" output="false" hint="My main event">
    <cfargument name="event">
    <cfargument name="rc">
    <cfargument name="prc">
    
    <cfset rc.result = getmodel("Math").add(rc.arg1, rc.arg2)>
    <cfset event.renderdata(data=rc.result)>
  </cffunction>

The Math.add method adds arg1 and arg2 and returns the result.
I added a testadd method to my GeneralTest.cfc integration test:
  <cffunction name="testadd" returntype="void" output="false">
    <cfscript>
    var event = "";
    URL.arg1 = 2;
    URL.arg2 = 3;
    event = execute("general.add");
    assertEquals(5, event.getValue("result",""), "Failed to assert result=5");
    </cfscript>
  </cffunction>
This works.

I added a testadd() method to TargetedGeneralTest.cfc:
  function testadd(){
    var rc = mockRequestContext.getCollection();
    var prc = mockRequestContext.getCollection(private=true);
    rc.arg1 = 2;
    rc.arg2 = 3;
    handler.add(mockRequestContext,rc,prc);
    assertEquals(5, rc.result, "Failed to assert result=5");
  }

This gets:
Variable CONTROLLER is undefined.
at line 116 of FrameworkSupertype.cfc which is the following line in
FrameworkSupertype.getModel:
<cfreturn controller.getWireBox().getInstance(argumentCollection=arguments)>

I took a stab at adding a controller reference to the handler via the
setup function:
  void function setup(){
    super.setup();
    handler.$property("controller","variables",mockController);
  }

Now, I get the following error:
The method getInstance was not found in component
C:\inetpub\wwwroot\coldbox\system\ioc\Injector.cfc.

I appears that my controller reference found getWireBox() which found
ioc/Injector.cfc which certainly defines getInstance() so I'm a little
baffled by this error.

Can anyone get me back on track?

Thanks,
Bob

Hi Bob

Will also be interested in the reply you get as I have always found targeted unit testing of controllers difficult to get working.

In the past I've had long conversations with people about how to approach it but never come up with a work solution

Robert

Bob,

Here is the issue and misconception. There are two types of tests that might be very similar but not really :slight_smile:

  1. Handler Targeted Testing
  2. Integration Testing

Handler Targeted Testing involves the testing of the handler CFC in isolation. Meaning any connection to anything in the ColdBox application context is disconnected by design, so you can use Mocking via MockBox to test any dependencies the Handler might have. This is to provide an isolated and targeted testing environment for the handler CFC.

Integration Testing is the testing of the handler CFC in real operating application context. Meaning everything is connected : caching, WireBox, settings, etc. I would say 98% of the time, you will want to do integration testing, as pretty much everything you will be testing is alive and ready for usage. The 2% is for the handler targeted testing, which usually means you are testing some kind of logic in the handler that needs isolation for testing.

As far as I can see from your test, you would be better off using integration testing, which in turns, really simplifies your tests too :slight_smile:

I hope this helps.

Luis F. Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Social: twitter.com/lmajano facebook.com/lmajano