Dependency question with MightyMock

14 views
Skip to first unread message

Tom Bishop

unread,
Jan 9, 2012, 1:13:33 PM1/9/12
to mxunit
Hi,

I am fairly new to mxunit but have a problem that I can't see the
answer to. I am using the MightyMock framework.

I am testing a service cfc which makes a call to the database. I have
used a mock to create a mock DAO which I have injected into the
service cfc via the setDAO method.

This appears to be working OK.

The method in the service cfc then calls the create() method in the
DAO which is supposed to return an instance of the object as a bean.

This also appears to be OK.

Then the service cfc method calls another method which is supposed to
send an email based on the object returned by the create() method.
However, it is throwing an error saying that the object passed to the
sendUpdatedTaskMessage() method is not of the correct type.

How do I force the create() method to return a valid bean/entity
object that can be passed to the sendUpdatedTaskMessage() method?

Is this possible with MightyMock?

Code below:

The Test:

<cffunction name="testSaveExceptionAsTask" returntype="void"
access="public">
<cfscript>
var task = "";
formScope = {
taskID=0,
systemID=1,
workflowID="1",
requestingPersonID=12345,
personID=2,
estimate=5,
actual=0,
typeID=1,
events=469,
importanceID=1,
assignedPersonID=2,
assignedCoPersonID=3,
name="Test Exception As Task",
dateDue="#now()+5#",
priority=0,
isFirmDeadline=false,
milestoneID=0,
note="Test Note"
};
var task = variables.taskService.createTaskBean(formScope);
var taskDAO =
mock('intranet.it_tickets.app.models.task.TaskDAO','typeSafe');
variables.taskService.setTaskDAO(taskDAO);
task = variables.taskService.saveExceptionAsTask(formScope);
debug(task);
</cfscript>
</cffunction>

The service layer method:

<cffunction name="saveExceptionAsTask" access="public"
returntype="intranet.it_tickets.app.models.task.Task" output="false"
hint="Creates a new task from data contained in an exception in the
sssdata.appExceptions table.">
<cfargument name="formScope" type="struct" required="true">
<cfset var task = createTaskBean(formScope)>

<cfset var changeLog = "">

<cfif task.isValidated()>
<cfif task.getTaskID() gt 0>
<cfset task = INSTANCE.com.taskDAO.update(task)>
<cfset task.buildChangeLog()>
<cfif task.hasChangeLog()>
<cfset INSTANCE.com.messageService.sendUpdatedTaskMessage(task)>
</cfif>
<cfelse>
<cfset task = INSTANCE.com.taskDAO.create(task)>
<cfset INSTANCE.com.messageService.sendNewTaskMessage(task)>
</cfif>
<cfif task.hasPriority()>
<cfset reconcilePriorities()>
</cfif>
</cfif>

<cfreturn task>
</cffunction>

Any help would be greatly appreciated.
Tom

Bob Silverberg

unread,
Jan 9, 2012, 1:30:37 PM1/9/12
to mxu...@googlegroups.com
This is just off the top of my head, so it may not be 100% accurate,
but I wanted to point out that you are missing a step. After you
create the mock taskDAO, you need to tell that mock how to respond to
the create() method. If you want it to return an object of a
particular type, then you need to mock the create() method to do that.
Now, you may actually want it to return another mock, which would
look something like this:

mockTask = mock('intranet.it_tickets.app.models.task.Task','typeSafe');
<--- guessing at the Task component type here
taskDAO.create('{*}').returns(mockTask);

The above may not give you exactly what you're looking for, but the
key is that second line there. You need to tell the taskDAO mock that
it needs to return something when the create() method is called on it.

Hopefully the above at least gets you over the current hurdle.

Cheers,
Bob

> --
> You received this message because you are subscribed to the Google Groups "mxunit" group.
> To post to this group, send email to mxu...@googlegroups.com.
> To unsubscribe from this group, send email to mxunit+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mxunit?hl=en.
>

--
Bob Silverberg
www.silverwareconsulting.com

Tom Bishop

unread,
Jan 9, 2012, 2:41:44 PM1/9/12
to mxunit

Hi Bob,

Thanks very much for that. You were spot on and it worked!!

Best regards,
Tom
Reply all
Reply to author
Forward
0 new messages