ColdFusion Unit Testing With MXUnit and TextMate

Written by

This post will show you how to get quickly up and running with ColdFusion, TextMate and MXUnit. As an example I will show you how to create a simple CFC with its corresponding unit test using MXUnit and then run the test locally within TextMate’s preview window.

1) Install TextMate:

macromates.com

2) Install the GetBundles tmbundle:

github.com/adamsalter/GetBundles.tmbundle

3) In TextMate:

Bundles > GetBundle > Install Bundle.

This will generate a list of available bundles, scroll down the list to ColdFusion and follow instructions to install.

4) Download and follow setup instructions for MXUnit (mxunit.org).

5) Create a new directory in your ColdFusion root and name it example_project.

6) You can now either open TextMate and drag the example_project onto the dock icon, or in Terminal cd to example_project and type:

1
mate .

7) Create an Application.cfc in the root of example_project by right clicking on the example_project directory in TextMate’s gutter and enter the following:

1
2
3
4
5
<cfcomponent output="false">
  <!--- set up per application mappings --->
  <cfset this.mappings = {}>
  <cfset this.mappings["/com"] = GetDirectoryFromPath( GetCurrentTemplatePath() ) & "com">
</cfcomponent>

8) Create a new directory in example_project and name it com.

9) Within the newly created com directory create a new file named App.cfc and enter the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<cfcomponent>
    <cffunction name="addRapper" 
              access="remote"
              returntype="any">
      <cfargument name="rapperName" 
                  type="string" 
                  required="true" 
                  hint="Name of a rapper">
      <cfscript>
          var dummyResult=StructNew();
          dummyResult.name=rapperName;
            return dummyResult;
      </cfscript>
  </cffunction>
</cfcomponent>

10) Create the following package structure:

example_project > test

11) Within the test directory create AppTest.cfc and enter the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<cfcomponent extends="mxunit.framework.TestCase">
  <cffunction name="setUp" returntype="void" access="public" hint="Setup prior to running tests">
      <cfset obj = createObject("component","com.App")>
  </cffunction>
  <cffunction name="tearDown" returntype="void" access="public" hint="Tear down post running tests">
      <!--- cleanup --->
  </cffunction>
  <cffunction name="testAddRapper" returntype="void" access="public">
      <cfscript>
          var rapperName="Kool Keith";
          var result = obj.addRapper(rapperName);
          debug(result);
          assertEquals(rapperName,result.NAME);
      </cfscript>
  </cffunction>
</cfcomponent>

12) In TextMate go to Bundles > Bundle Editor > Show Bundle Editor.

13) Select HTML from the list, click on the bottom left + icon in the Bundle Editor selecting New Command.

14) Enter the following command text amending the url to point to your ColdFusion localhost:

1
2
echo "<meta http-equiv='Refresh' content='0;
    URL=http://localhost/~$USER${TM_FILEPATH#$HOME/Sites}?method=runTestRemote'>"

Run Test CommandRun Test Command

15) Ensure the AppTest.cfc class is open and hit your shortcut keys specified for the newly created command to see the below window

MXUnit ResultMXUnit Result

Comments