You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to mxunit
I've got a problem that I don't have a clean solution for and could use some pointers on a better way to structure my code or test. I have a method that dynamically creates and returns an object, getBalance(). getBalance() is called by the method I want to test, settle()
I can create a mock for getBalance() to return, but I'm not sure the best way to inject it into the CUT. If my mock method creates the mock, it throws an error because mock() is not defined in the CUT, only in the unit test of course. The service method looks something like:
<cffunction name="settle">
<cfset result = getStripeBalance() /> <cfloop array="#result.getParsedResult()#" index="local.balance"> // do stuff </cfloop>
</cffunction>
I need to change what getParsedResult() returns when testing settle() (because it calls a remote third party service, Stripe). The strategy I'm using in my test which seems crappy is:
// do testing that calls a method settle() which internally calls getStripeBalance() which returns the public mocked object this.mock_balance. <cfset assertTrue(variables.service.settle() EQ true) />
</cffunction>
I don't like that I have to define a static variable name in my mock and then re-use it in my test method. Is there a better way?
Brian
Marc Esher
unread,
Aug 7, 2015, 3:15:23 PM8/7/15
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to mxu...@googlegroups.com
My initial reaction here is to not have settle() call getBalance(),
but to instead pass in the Balance object as an argument to settle.
By using dependency injection, you get back full control of your test.
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to mxunit
On Friday, August 7, 2015 at 12:15:23 PM UTC-7, Marc Esher wrote:
My initial reaction here is to not have settle() call getBalance(),
but to instead pass in the Balance object as an argument to settle.
By using dependency injection, you get back full control of your test.
Hrmmm... thanks for that Marc. I don't want my controllers to be fetching the balance and passing it in because we deal with multiple payment gateways and I want to abstract that from the app into a simple nightly batch process, but I could refactor the methods inside of settle() to achieve the same thing. Maybe:
<cffunction name="settle">
<cfset result = getStripeBalance() />
<cfset settleStripe(result) />
</cffunction>
And then I can test my settleStripe separately? Thanks,
Brian
Marc Esher
unread,
Aug 18, 2015, 6:35:02 PM8/18/15
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to mxu...@googlegroups.com
Brian,
That makes sense to me. Bottom line is that no matter where it happens
(service layer, controllers, whatever), you can't get full control
over your tests if the settle() method doesn't enable its inputs to be
changed at test time.