2012-10-12 33 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>