首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用MXUnit模拟/存根组件

使用MXUnit模拟/存根组件
EN

Stack Overflow用户
提问于 2012-10-12 22:44:03
回答 2查看 567关注 0票数 4

我有一个名为ComponentUnderTest.cfc的组件,如下所示:

代码语言:javascript
复制
<cfcomponent output="false">
<cfset externalComponent = Component("Externalcomponent");

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

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

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

发布于 2012-10-13 05:42:57

您必须将模拟的组件注入到componentUnderTest中,以替换现有的。

你可以这样做:

代码语言:javascript
复制
// 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
EN

Stack Overflow用户

发布于 2014-08-05 05:41:01

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12861514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档