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

Did you miss the News? Introducing the SocketBox STOMP Broker (Demo)

Did you miss the News? Introducing the SocketBox STOMP Broker (Demo)

We’ve been working on a fun new library to make WebSockets easier for CFML (and BoxLang) developers. WebSockets are incredibly powerful for real-time applications, but CF developers have historically lagged behind other languages in adopting this technology. With SocketBox, we’re addressing this gap by providing a solution that is easy to implement, much like Socket.io for Node.js apps.

Maria Jose Herrera
Maria Jose Herrera
November 25, 2024
BoxLang 1.0.0 Beta 23 Launched

BoxLang 1.0.0 Beta 23 Launched

The latest release of BoxLang, Beta 23, marks a significant step forward in our journey to create the ultimate dynamic language for the JVM. Packed with powerful new features, important bug fixes, and thoughtful optimizations, this update is designed to make your development experience smoother, faster, and more reliable, especially after now starting to take 100s of comments and bug reports from our community.

Luis Majano
Luis Majano
November 23, 2024
TestBox Latest Updates and News!

TestBox Latest Updates and News!

We’re thrilled to have launched the new TestBox website and TestBox 6.0! If you haven’t had a chance to explore yet, visit TestBox to discover updated documentation, powerful resources, and features that make testing more efficient than ever.

Maria Jose Herrera
Maria Jose Herrera
November 21, 2024