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();
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.
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?