2012-10-12 34 views
4

存根组件我一直在寻找一个叫ComponentUnderTest.cfc成分为:模拟/与MXUnit

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

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

我如何可以模拟/存根externalComponent.externFunction()在MXUnit测试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> 
+1

http://wiki.mxunit.org/display/default/Defining+a+Mock%27s+Behaviour – Henry

+0

这并没有真正与得到的模拟*为帮助* ComponentUnderTest实例虽然... –

回答

0

您将不得不将模拟组件注入componentUnderTest以替换现有的组件。

你可以做这样的事情:

// 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); 

事情本该刚提供的返回值时ExternalFunction()被称为返回MockForExternalFunction(),它不叫代替ExternalFunction的()。虽然这应该没问题。

+0

我想检查我的MockForExternalFunction是否被调用,当它应该是和多少次。 –

+0

这可能是一个单独的问题。我知道人们可以用Mockbox做这个,不知道MXUnit。使用MockBox,您可以看到调用了多少次'externalFunction()',而不是'mockForExternalFunction()'。 A)当然,它等同于相同的事物; B)这是你感兴趣的实际方法,而不是嘲讽的方法。 –

0
<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> 

Inject Method

<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>