I have a component called ComponentUnderTest.cfc looking as:
<cfcomponent output="false">
<cfset externalComponent = Component("Externalcomponent");
<cffunction name="FunctionUnderTest" access="public"...>
<cfset externalComponent.ExternalFunction()>
</cffunction>
</cfcomponent>
How can I mock/stub externalComponent.externFunction() in a MXUnit test componenent:
<cfcomponent displayname="ComponentTester" extends="mxunit.framework.TestCase>
<cffunction name="MockForExternalFunction">
.....
</cffunction>
??????
<cffunction name=TestComponent>
<cfset componentUnderTest = CreateObject("ComponentUnderTest")>
?????
<cfset componentUnderTest.FunctionUnderTest()> <!--- should call MockForExternalFunction --->
</cffunction>
</cfcomponent>
You're going to have to inject the mocked component into
componentUnderTest
to replace the existing on.You can do something like this:
The thing with this that
MockForExternalFunction()
just provided the return value to return whenExternalFunction()
is called, it's not called instead of ExternalFunction(). This should be fine, though.