I have an implementation of a storage interface for 
github.com/RangelReale/osin that I would like to be able to test each 
function in isolation. One of the aspects of the interface is that certain 
functions must return with data loaded that other functions are responsible 
for returning in the interface, which makes you have struct methods that 
call other struct methods.

So, you end up with dependency chains like LoadAccess must return Client 
information, so LoadAccess then depends on GetClient. To test each function 
in isolation, I wanted to be able to mock the call to GetClient, so I wrote 
out a little test mock like I have 
before: https://play.golang.org/p/P2-BDZDW9i

The problem with the test mock in this case is how it must both call the 
real implementation, as well as provide a mock. The call makes it so the 
later call to GetData doesn't go through the indirection layer of the 
storageMock object, it directly calls the underlying object, and therefore 
skips the indirection.

I asked golang-slack, and the response I got was to essentially have the 
implementation just be done like the mock, so your unit test could simply 
change the implementation and be able to pass functions similar to how I 
implemented the storageMock. I was wondering if anyone had another option, 
or is the best way to make this type of code testable to make it always 
have the indirection layer from struct method to struct field which is a 
func literal?

Thank you all.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to