Coldfusion 带有MXUnit的模拟/存根组件

Coldfusion 带有MXUnit的模拟/存根组件,coldfusion,mocking,components,stub,mxunit,Coldfusion,Mocking,Components,Stub,Mxunit,我有一个名为ComponentUnderTest.cfc的组件,看起来是: <cfcomponent output="false"> <cfset externalComponent = Component("Externalcomponent"); <cffunction name="FunctionUnderTest" access="public"...> <cfset externalComponent.ExternalFunction

我有一个名为ComponentUnderTest.cfc的组件,看起来是:

<cfcomponent output="false">
<cfset externalComponent = Component("Externalcomponent");

  <cffunction name="FunctionUnderTest" access="public"...>
     <cfset externalComponent.ExternalFunction()> 
  </cffunction>
</cfcomponent>


您必须将模拟组件注入
componentUnderTest
以替换现有的on

您可以这样做:

// I took this lot from the docs Henry pointed you to: I'm not familiar with MXUnit's mocking framework, but this sounds right
mockedObjectWithMockedMethod = mock();
mockedObjectWithMockedMethod.ExternalFunction().returns(MockForExternalFunction());

function injectVariable(name, value){
    variables[name] = value;
}
componentUnderTest.injectVariable = injectVariable;
componentUnderTest.injectVariable("externalComponent", mockedObjectWithMockedMethod);

问题是,
MockForExternalFunction()
只提供了调用
ExternalFunction()
时要返回的返回值,而不是调用ExternalFunction()。不过,这应该没问题。

这对将mock放入ComponentUnderTest实例并没有真正的帮助……我想检查MockForExternalFunction是否在应该调用的时候被调用以及调用了多少次。这可能是一个单独的问题。我知道有人可以用Mockbox做到这一点,但对MXUnit一无所知。使用MockBox,您可以看到调用了多少次
externalFunction()
,而不是
mockForExternalFunction()
。A) 它肯定等同于同一件事;B) 这是您感兴趣的实际方法,而不是模拟方法。
<cfcomponent displayname="ComponentTester" extends="mxunit.framework.TestCase>

 <cffunction name="MockForExternalFunction">
   .....
 </cffunction>

 <cffunction name=TestComponent>
     <cfset componentUnderTest = CreateObject("ComponentUnderTest")>
     <cfset injectMethod(componentUnderTest, this, "MockForExternalFunction", "FunctionUnderTest") />
     <cfset componentUnderTest.FunctionUnderTest()>  <!--- should call MockForExternalFunction --->
 </cffunction>
</cfcomponent>
<cfcomponent displayname="ComponentTester" extends="mxunit.framework.TestCase>

 <cffunction name="MockForExternalFunction">
   .....
 </cffunction>

 <cffunction name=TestComponent>
     <cfset componentUnderTest = CreateObject("ComponentUnderTest")>
     <cfset injectMethod(componentUnderTest, this, "MockForExternalFunction", "FunctionUnderTest") />
     <cfset componentUnderTest.FunctionUnderTest()>  <!--- should call MockForExternalFunction --->
 </cffunction>
</cfcomponent>
<cffunction name="injectMethod" output="false" access="public" returntype="void" hint="injects the method from giver into receiver. This is helpful for quick and dirty mocking">
    <cfargument name="Receiver" type="any" required="true" hint="the object receiving the method"/>
    <cfargument name="Giver" type="any" required="true" hint="the object giving the method"/>
    <cfargument name="FunctionName" type="string" required="true" hint="the function to be injected from the giver into the receiver"/>
    <cfargument name="FunctionNameInReceiver" type="string" required="false" default="#arguments.functionName#" hint="the function name that you will call. this is useful when you want to inject giver.someFunctionXXX but have it be called as someFunction in your receiver object">
</cffunction>