Blog

Curt Gratz

March 19, 2010

Spread the word


Share your thoughts

MockBox can have many amazing uses.  One well documented use is in assistance in creating complicated unit tests insuring you are only testing one small unit of work, however it can be used for many other interesting use cases.

One of the ways we find MockBox useful during our development cycle at Computer Know How is to mock objects we haven't had the time to complete yet, but we do know what we expect as response.  This allows us to continue development without waiting for the piece of something we haven't done yet slow us down, but keep the method calls exactly as they will be in when the object is complete.

So, how do you use MockBox outside of the context of a unit test?  Well, its easy.  

Inside a ColdBox application.

//get an instance of mockbox to use for mocking things not 100% built yet inside a ColdBox app mockBox = createObject("component","coldbox.system.testing.MockBox").init();

Outside a ColdBox application.

//get an instance of mockbox to use for mocking things not 100% built yet outside a ColdBox app. mockBox = createObject("component","mockbox.system.testing.MockBox").init();

Now that MockBox is initialized, we can start mocking objects. Lets say we have a User Object that we haven't had time to build yet. Right now it looks really advanced with lots of cool properties and functions that we spent a ton of time on. Something like this.

cfcomponent hint="I am the User ORM cfc" /cfcomponent

Now what if we needed to build a Welcome screen based off this User Object. Well, we could do something like this.

var rc = event.getCollection(); //get the users name, later we will need to switch this to get it from the user object. rc.Name = "Paul Was Saul"; But later, we would have to implement our user object once we are done with it. But what if we mocked our user object with the getName method it will ultimately have. var rc = event.getCollection(); //get an instance of mockbox var mockBox = createObject("component","coldbox.system.testing.MockBox").init() //Mock a User Object since its not built yet. var User=mockBox.createMock(className="model.ORM.User",clearMethods=true,callLogging=true); User.$(method="getName",returns="Paul Was Saul"); //get the users name rc.Name = User.getName();

Now, I know what your thinking. The non-mocked version looks a lot simpler then the mocked version. Well that is probably true in this simple example, but use your imagination and expand this example using the power of your mind and you can see how handy MockBox can be in your own development.

Want to learn more about MockBox. Read the wiki. As with everything done by Luis Majano, the documentation is outstanding.

http://wiki.coldbox.org/wiki/MockBox.cfm

Or Download it at

http://www.coldbox.org/download

Yet another tool to put in your toolbox to make your life simpler brought to you by Team ColdBox.

Add Your Comment

(2)

Mar 19, 2010 10:53:11 UTC

by Luis Majano

Cool approach Curt, My head started spinning and could be cool to have a mock interceptor that sets up all the mocks you would need for your session until objects get build out. This way you can put all your mock code in this single object and be coding it out. Very interesting approach. PS: you don't need to do: rc = event.getCollection() on your views/layouts. That's done implicitly :)

Mar 19, 2010 10:58:59 UTC

by Curt Gratz

Thanks man. In reality I do it in the mocks preHandler interceptor so its available to me when I need it. PS: those are are meant to be handlers, I don't think it does it implicitly there.

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