[mockbox] Mocking createObject

I’m trying to test a method that includes a createObject call:

local.foo = createObject("component", "path.to.component");

I’m attempting to mock the createObject call as follows, but my mocked createObject is being ignored.

local.mocked.theObj = createEmptyMock("path.to.component").$("getSomething"); variables.CUT.$property("createObject", "this", local.mocked.theObj);

Is there another way of mocking createObject?

I’m unclear on what you are trying to do. Your call to $property will create a variable called “createObject” in the this scope of CUT. It will NOT override the built in function createObject().

There is no way to mock the creation of local.foo since you are using a direct createObject() call directly.

My first recommendation is that you use an object factory/DI engine like Wirebox to create your dependencies.

// WireBox can be mocked here
local.foo = wirebox.getInstance(‘component’);

The second option is to at least move the creation of local.foo into a method:

// getFoo() can be mocked here
local.foo = getFoo();



ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

Thanks for your response Brad. After some investigation we discovered that it wasn’t possible to mock built-in functions. I agree that using a DI framework would be the best approach, but at the moment we’re looking to improve the test coverage of existing code without modifying it. Hopefully we’ll be able to refactor the code and improve the test coverage in the near future. For the interim I’m planning to add an integration test for the method I’m testing.


I added a way to mock internal functions in the development branch. It worked locally for me. Can you try updating to our development branch?


Luis F. Majano
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Social: twitter.com/lmajano facebook.com/lmajano

Sure, I’ll give it a try on Monday. Thanks Luis.


Do you have an example of how to do this? I am doing something like the following:

//Test Method

function testGetTypeReturnsFormType() {
mockBox.createStub().$( “FileReadBinary”, “SomeFakeData” );

var result = indexService.addDocumentData(new IndexItem());

//The method to be tested
public IndexItem function addDocumentData(required IndexItem indexItem) {
/* Some logic here */
var filePath = pathService.getFilePath();
indexItem.setCustomField(‘FileContent’, FileReadBinary(filePath));
return indexItem;

However when I do this I still get a network path not found error since the file doesn’t exist. How does does the framework know to call the mocked version of ReadFileBinary instead of the built in one?