【问题标题】:mock/stub Component with MXUnit使用 MXUnit 模拟/存根组件
【发布时间】:2012-10-03 09:55:29
【问题描述】:

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

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

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

如何在 MXUnit 测试组件中模拟/存根 externalComponent.externFunction():

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

【问题讨论】:

标签: coldfusion mocking components stub mxunit


【解决方案1】:

您将不得不将模拟组件注入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);

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

【讨论】:

  • 我想检查一下我的 MockForExternalFunction 是否在应该调用的时间以及调用了多少次。
  • 这可能是一个单独的问题。我知道可以用 Mockbox 做到这一点,但不知道 MXUnit。使用 MockBox,您可以看到调用了多少次 externalFunction(),而不是 mockForExternalFunction()。 A)当然,它等同于同一件事; B)这是您感兴趣的实际方法,而不是模拟方法。
【解决方案2】:
<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多