Testing private methods
Knowing that a private method inside a class is working, is something that is valuable on occasion. However, how do you test a method that is private? Can this be done? Well the bright folks involved in the MXUnit library have figured it out. Add another method to the math.cfc
file we created earlier in the chapter:
<cffunction name="echo" access="private" output="false"> <cfargument name="value"> <cfreturnARGUMENTS.value> </cffunction>
Now we need to create a test to check and see if we can test this private method. There is a special function in MXUnit called makePublic()
. If a function is private, you cannot test it without using this method. Create a file called privateTest.cfc
and place the following code in that file:
<cfcomponentdisplayname="assertTest2" extends="mxunit.framework.TestCase"> <cffunction name="getPrivateTest"> <cfscript> var math = createObject("component","share.tests.cf9dt.math"); ...