Hello Guys,
Recently I have started working with Mockbox and stuck at one level.
Basically I am trying to create unit test for service layer where
mocking DAO object. Mocking work great for me but in "Create" method
of DAO I have passed bean as argument and function return true/false
also this function add record into database and assign auto generated
id back to passed arguments so I can use in service layer.
But I am not sure how to mock such method so my passed bean object in
unit test have id that I want in mock.
Thanks
Pritesh
Can you post the service layer code in question please
Below is contentDAO and contentService code I have just kept only
require to add content. Basically I want to store id in event table
whenever new content is added into database.
<cfcomponent displayname="contentDAO" hint="table ID column = id">
<cffunction name="create" access="public" output="false"
returntype="boolean">
<cfargument name="content" type="content" required="true" />
<cfset var qCreate = "" />
<cftry>
<cfquery name="qCreate" datasource="#variables.dsn#">
INSERT INTO content
(status, html)
VALUES
(
<cfqueryparam value="#arguments.content.getstatus()#"
CFSQLType="cf_sql_bit" null="#not
len(arguments.content.getstatus())#" />,
<cfqueryparam value="#arguments.content.gethtml()#"
CFSQLType="cf_sql_longvarchar" null="#not
len(arguments.content.gethtml())#" />
)
select @@identity as id
</cfquery>
<cfset arguments.content.setId(qCreate.id)>
<cfcatch type="database">
<cfreturn false />
</cfcatch>
</cftry>
<cfreturn true />
</cffunction>
</component>
<cfcomponent name="contentService" output="false" cache="true"
cachetimeout="30">
<cfproperty name="contentDAO" inject="model:contentDAO"
scope="instance">
<cfproperty name="contentGateway" inject="model:contentGateway"
scope="instance">
<cfproperty name="eventService" inject="model:eventService"
scope="instance">
<cffunction name="savecontent" access="public" output="false"
returntype="boolean">
<cfargument name="content" type="content" required="true" />
<cfset var issuccess =
instance.contentDAO.save(content)>
<cfif issuccess>
<cfset events.addNewEvent("New content added with
id:" & arguments.content.getId())>
</cfif>
<cfreturn issuccess />
</cffunction>
</cfcomponent>