Yvo*_*ers 5 coldfusion components mocking stub mxunit
我有一个名为 ComponentUnderTest.cfc 的组件,如下所示:
<cfcomponent output="false">
<cfset externalComponent = Component("Externalcomponent");
<cffunction name="FunctionUnderTest" access="public"...>
<cfset externalComponent.ExternalFunction()>
</cffunction>
</cfcomponent>
Run Code Online (Sandbox Code Playgroud)
如何在 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>
Run Code Online (Sandbox Code Playgroud)
您将必须将模拟的组件注入其中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);
Run Code Online (Sandbox Code Playgroud)
与此相关的东西MockForExternalFunction()只是提供了调用时返回的返回值ExternalFunction(),而不是调用它而不是ExternalFunction()。不过,这应该没问题。
| 归档时间: |
|
| 查看次数: |
635 次 |
| 最近记录: |