Creating a Mock Object
In order to create a mock object you need to use any of the following methods: createMock(), createEmptyMock(), or prepareMock().

createMock()

Used to create a new mock object from scratch or from an already instantiated object.
1
public any createMock([string CLASSNAME], [any OBJECT], [boolean CLEARMETHODS='false'], [boolean CALLLOGGING='true'])
Copied!
Parameters:
  • className - The class name of the object to create and mock
  • object - The instantiated object to add mocking capabilities to, similar to using prepareMock()
  • clearMethods - If true, all methods in the target mock object will be removed. You can then mock only the methods that you want to mock
  • callLogging - Add method call logging for all mocked methods only
1
collaborator = mockbox.createMock("model.myClass");
Copied!

createEmptyMock()

Used to create a new mock object with all its method signatures wiped out, basically an interface with no real implementation. It will be up to you to mock all behavior.
1
public any createEmptyMock(string CLASSNAME, [any OBJECT], [boolean CALLLOGGING='true'])
Copied!
Parameters:
  • className - The class name of the object to create and mock
  • object - The instantiated object to add mocking capabilities to, similar to using prepareMock()
  • callLogging - Add method call logging for all mocked methods only
1
user = mockbox.createEmptyMock("model.User");
Copied!

prepareMock()

Decorate an already instantiated object with mocking capabilities. It does not wipe out the object's methods or signature, it only decorates it (mixes-in methods) with methods for mocking operations. This is great for doing targeted mocking for specific methods, private methods, properties and more.
1
public any prepareMock([any OBJECT], [boolean CALLLOGGING='true'])
Copied!
Parameters:
  • object - The already instantiated object to prepare for mocking
  • callLogging - Add method call logging for all mocked methods only
1
myService = createObject("component","model.services.MyCoolService").init();
2
// prepare it for mocking
3
mockBox.prepareMock( myService );
Copied!
Caution If call logging is turned on, then the mock object will keep track of all method calls to mocked methods ONLY. It will store them in a sequential array with all the arguments the method was called with (named or ordered). This is essential if you need to investigate if a method was called and with what arguments. You can also use this to inspect save or update calls based on mocked external repositories.
Sample:
Let's say that we have a user service layer object that relies on the following objects:
  • sessionstorage - a session facade object
  • transfer - the transfer ORM
  • userDAO - a data access object for complex query operations
We can start testing our user service and mocking its dependencies by preparing it in a test case CFC with the following setup() method:
1
component extends=”testbox.system.BaseSpec” {
2
3
function beforeAll(){
4
//Create the User Service to test, do not remove methods, just prepare for mocking.
5
userService = createMock("model.UserService");
6
7
// Mock the session facade, I am using the coldbox one, it can be any facade though
8
mockSession= createEmptyMock(className='coldbox.system.plugins.SessionStorage');
9
10
// Mock Transfer
11
mockTransfer = createEmptyMock(className='transfer.com.Transfer');
12
13
// Mock DAO
14
mockDAO = createEmptyMock(className='model.UserDAO');
15
16
//Init the User Service with mock dependencies
17
userService.init(mockTransfer,mockSession,mockDAO);
18
}
19
20
function run(){
21
22
describe( "User Service", function(){
23
it( "can get data", function(){
24
// mock a query using mockbox's querysimulator
25
mockQuery = querySim("id, name
26
1|Luis Majano
27
2|Alexia Majano");
28
// mock the DAO call with this mocked query as its return
29
mockDAO.$("getData", mockQuery);
30
31
data = userService.getData();
32
expect( data ).toBe( mockQuery );
33
});
34
});
35
36
}
37
38
}
Copied!
The service CFC we just injected mocked dependencies:
1
<cfcomponent name="UserService" output="False">
2
3
<cffunction name="init" returntype="UserService" output="False">
4
<cfargument name="transfer">
5
<cfargument name="sessionStorage">
6
<cfargument name="userDAO">
7
<cfscript>
8
instance.transfer = arguments.transfer;
9
instance.sessionStorage = arguments.sessionStorage;
10
instance.userDAO = arguments.userDAO;
11
12
return this;
13
</cfscript>
14
</cffunction>
15
16
<cffunction name="getData" returntype="query" output="false">
17
<cfreturn instance.userDao.getData()>
18
</cffunction>
19
20
</cfcomponent>
Copied!
Last modified 3yr ago
Copy link
Edit on GitHub