Blog

Luis Majano

March 09, 2011

Spread the word


Share your thoughts

A few folks have asked how you can leverage MXUnit and MockBox to mock a super class class.  Here is a simple example to showcase this:

Class Parent A

<cfcomponent name="A"> 
<!--- send ---> 
<cffunction access="public" hint="" name="send" output="false" returntype="any"> 
<cfreturn parent="">
</cffunction> 
</cfcomponent> 

Class Parent B

<cfcomponent extends="A" name="B"> 
	<!--- send ---> 
	<cffunction access="public" hint="" name="send" output="false" returntype="any"> 
		<cfargument default="" hint="" name="name" required="true" type="string"> 
		<cfif arguments.name neq ""> 
			<cfreturn name="arguments.name"> 
		<cfelse> 
			<cfreturn>
		</cfif> 
	</cffunction> 
</cfcomponent>

Here is our test case, please note that we use the coldbox package for simplicity, if you are using mockbox standalone, then just follow the docs.

<cfcomponent extends="coldbox.system.testing.BaseTestCase">
<cfscript> 
function setup(){ 
    // create and prepare for mocking. 
    b = getMockBox().prepareMock( createObject("component","B") ); 
} 

function testSend(){ 
    // create a virtual super scope and mock a send method on it 
    mockSuper = getMockBox().createStub().$("send","Mocked Super"); 
    
    // inject the mock super into the variables scope 
    b.$property("super","variables", mockSuper); 
    
    // send method call 
    r = b.send(); 
    
    // use simple mxunit debug to show results. 
    debug(r); 
} 
</cfscript>
</cfcomponent>

So as you can see, the super scope is just another reference in the variables scope, so I can easily create a MockBox stub and mock away. You can also be more concise if you want to leave the methods intact in the super scope by creating a mock of the parent class:

getMockBox().createMock("A").$("send","Mocked Super");

 

Add Your Comment

(2)

Mar 10, 2011 09:03:09 UTC

by Chris Tierney

Thanks Luis for this great write up! What is you opinion on mocking a super class? Should it be mocked as the example provides or should it generally be treated as an all-in-one?

May 14, 2013 12:43:43 UTC

by duncan

Thanks for this useful post. It's a shame the blog has screwed up the layout of the code block. Also the date of the post is reading as 1111 for the year

Recent Entries

ColdBox 7.2.0 Released

ColdBox 7.2.0 Released

ColdBox, a widely used development platform for ColdFusion (CFML), has unveiled version 7.2. Packed with compelling new features, bug fixes, and enhancements, this release is designed to empower developers by boosting productivity, refining scheduled task capabilities, and enhancing the overall reliability and efficiency of application development. This article will delve into the key highlights of ColdBox 7.2 and elucidate how these advancements can positively impact developers in their daily coding endeavors.

Luis Majano
Luis Majano
November 20, 2023
Into the Box 2023 Series on CFCast

Into the Box 2023 Series on CFCast

Excitement is in the air as we unleash the highly anticipated ITB 2023 series exclusively for our valued CFCast subscribers – and the best part? It's FREE for CFCast members! Now is the perfect time if you haven't joined the CFCast community yet. Plus, we've got an incredible End-of-Year deal that's too good to miss

Maria Jose Herrera
Maria Jose Herrera
November 20, 2023
Ortus Deals are Finally Here!

Ortus Deals are Finally Here!

The much-anticipated Ortus End-of-the-Year Sale has arrived, and it's time to elevate your development experience! Whether you're a seasoned developer, a tech enthusiast, or someone on the lookout for top-notch projects, Ortus has something special in store for you. Brace yourself for incredible discounts across a wide array of products and services, including Ortus annual events, books, cutting-edge services, and more.

Maria Jose Herrera
Maria Jose Herrera
November 15, 2023