injectMethod, will it help me?

15 views
Skip to first unread message

fmdano

unread,
Dec 19, 2012, 11:32:10 AM12/19/12
to mxunit
Hey,
so I have this in my component function:
<cffunction name="doTransfer" access="public" returntype="struct"
output="false" hint="Change owner to cases.">

<cfargument name="caseid" type="string" required="yes" default="">
<cfargument name="new_owner_user_id" type="string" required="yes"
default="">
<cfargument name="application_id" type="numeric" required="yes"
default="2">
<cfargument name="form_id" type="numeric" required="yes"
default="2">

<cfscript>
var dataStruct = structNew();
dataStruct.data = '';
dataStruct.error = '';
</cfscript>

<cfset new_owner_username = #qryNewOwner.username#>

This cfset above is using a function on this page to get the username
information. Can i use the injectMethod function to pass into this
page the new_owner_username info, or maybe because this is a variable,
can i use injectProperty?

these are new functions to me (just found them in documentation), so
not sure where they are best suited?

thanks
dan

Marc Esher

unread,
Dec 22, 2012, 3:11:47 PM12/22/12
to mxu...@googlegroups.com
On Wed, Dec 19, 2012 at 11:32 AM, fmdano <fmd...@gmail.com> wrote:
Hey,
so I have this in my component function:
<cffunction name="doTransfer" access="public" returntype="struct"
output="false" hint="Change owner to cases.">

                <cfargument name="caseid" type="string" required="yes" default="">
                <cfargument name="new_owner_user_id" type="string" required="yes"
default="">
                <cfargument name="application_id" type="numeric" required="yes"
default="2">
                <cfargument name="form_id" type="numeric" required="yes"
default="2">

                <cfscript>
                        var dataStruct = structNew();
                        dataStruct.data = '';
                        dataStruct.error = '';
                </cfscript>

                <cfset new_owner_username = #qryNewOwner.username#>

This cfset above is using a function on this page to get the username
information. Can i use the injectMethod function to pass into this
page the new_owner_username info, or maybe because this is a variable,
can i use injectProperty?


I don't see where you're creating this qryNewOwner variable, but presumably that's coming from some function, something like:

<cfset var newOwner = getNewOwner(arguments.new_owner_user_id)>


If that's the case, and what you want to do is mock that method call rather than have it actually do a lookup, then yes, you could use injectMethod to inject a new getNewOwner() method which presumably simply returns some static value


Most likely in your test, you'd have this:

<cffunction access="private" name="getNewOwnerOverride">
 <cfset var q = queryNew('col1,col2,col3')>
 .... do stuff which sets up that fake query
  <cfreturn q>
</cffunction>

And then in your test method, you'd have something like:

<cfset injectMethod( objectUnderTest, this, "getNewOwnerOverride", "getNewOwner")>

Which would overwrite your objectUnderTest's getNewOwner method with the getNewOwnerOverride method that lives in your testcase.

 

these are new functions to me (just found them in documentation), so
not sure where they are best suited?

thanks
dan

--
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.


fmdano

unread,
Dec 31, 2012, 10:29:32 AM12/31/12
to mxunit
Marc,
thanks so much for this...I have issues like this all over our code,
and I assumed i'd have to do some mocking, but I have not really
understood how to do simple mocks, so what you have shown me here is
the first time I have seen a simple mock...if you have any other
simple mocking examples, please pass them on to me...if I have other
questions on this, i hope you'll be able to help me.

Dan

Marc Esher

unread,
Jan 2, 2013, 6:14:14 AM1/2/13
to mxu...@googlegroups.com
Dan,

Glad to help. For a longer explanation of injectMethod, see here: http://wiki.mxunit.org/display/default/Using+injectMethod+for+simple+mocking

I use this approach all the time when I need to override, at test time, the behavior of a single method.

As you come across more questions, let us know.

Marc
Reply all
Reply to author
Forward
0 new messages