Re: [mxunit:3634] Mocking Dependencies in local scope

57 views
Skip to first unread message

James Holmes

unread,
Jun 20, 2012, 8:24:49 PM6/20/12
to mxu...@googlegroups.com
Can you consider altering the architecture so that the object (or at least the method) takes in the instance of someOtherObj? If you need new instances each time, perhaps the object can take in a factory that creates them instead. This will give you loose coupling and allow for easy testing.

--
Shu Ha Ri: Agile and .NET blog
http://www.bifrost.com.au/


On 21 June 2012 10:08, Richard Mueller <rbmu...@gmail.com> wrote:
Hi - I'm trying to mock a dependency in one of my methods, which is currently assigned to the local scope, and I'm having trouble using the injectProperty method to achieve this.  I'm fairly new to Mighty Mock so I can't be certain I'm missing something obvious.

<cffunction name="foo">
    <cfset local.bar = CreateObject("component", "someOtherObj") />
//or how about?
    <cfset someOtherFunction(CreateObject("component", "myOtherObj") />
</cffunction>

Any thoughts/techniques on either of these?

Thanks

Richard

--
You received this message because you are subscribed to the Google Groups "mxunit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mxunit/-/ZQKKIiyG-i4J.
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.

Marc Esher

unread,
Jun 20, 2012, 8:37:38 PM6/20/12
to mxu...@googlegroups.com
On Wed, Jun 20, 2012 at 8:24 PM, James Holmes <james....@gmail.com> wrote:
> Can you consider altering the architecture so that the object (or at least
> the method) takes in the instance of someOtherObj? If you need new instances
> each time, perhaps the object can take in a factory that creates them
> instead. This will give you loose coupling and allow for easy testing.

Right on: if it's a dependency, make it injectable, either directly or
via a factory

Failing that, the next best thing is to mimic the factory approach and
do something like this:

<cffunction name="foo">
<cfset local.bar = getBar() />

</cffunction>

<cffunction name="getBar">
<cfreturn CreateObject("component", "someOtherObj")>
</cffunction>

And then in your test, you can use injectMethod() to override the
getBar method to have it return the object you want.

But the fact that you need to do that is a huge red flag that
dependency injection is the answer here.

Marc

Richard Mueller

unread,
Jun 20, 2012, 11:10:46 PM6/20/12
to mxu...@googlegroups.com
We are not using and IoC framework just yet so is it feasible to just set accessors="true" then create a someOtherObj property.
Then before the original obj init, use myObject.setSomeOtherObj ( CreateObject("component", "someOtherObj") ) ?  Making foo look a little more like

<cffunction name="foo"> 
 <cfset local.bar = variables.someOtherObj.doSomething() />
</cffunction>

Otherwise I think the init may get a bit messy with objects never being used.
>> For more options, visit this group at
>> http://groups.google.com/group/mxunit?hl=en.
>
>
> --
> 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

Marc Esher

unread,
Jun 21, 2012, 9:42:58 AM6/21/12
to mxu...@googlegroups.com
exactly
>> >> mxunit+un...@googlegroups.com.
>> >> For more options, visit this group at
>> >> http://groups.google.com/group/mxunit?hl=en.
>> >
>> >
>> > --
>> > 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.
>
> --
> You received this message because you are subscribed to the Google Groups
> "mxunit" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/mxunit/-/r_8y3Z91n7EJ.
>
> To post to this group, send email to mxu...@googlegroups.com.
> To unsubscribe from this group, send email to
> mxunit+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages