On Thu, 12 Dec 2019, at 10:27 PM, karthik3...@gmail.com wrote:
> Specifically, I couldn't find how to partially mock the `struct`. Most 
> suggestions revolve around breaking such `struct`s but in this case, the 
> `struct` is already at its bare minimum. It seems like a fair ask to not 
> break the `ResourceManager` interface (into single and multi) for the sake of 
> testing as that would not make much sense and would be kludgy at best and 
> wouldn't scale well as more such methods make into the interface.


I think your problems likely stem from having interfaces that are too large. 
Interfaces in Go are more flexible than in many other languages since there is 
no "implements" keyword that binds a struct to a particular interface. This 
means that you can have many more specialised interfaces that your struct may 
automatically satisfy.

One consequence of this is that In Go it is usually preferable to define the 
interface where it is consumed instead of where the implementations are. This 
tends to make interfaces smaller, having just the methods needed for the 
consumer. It also implies that testing via the interface is the wrong approach.

First write your tests against the struct to verify that getting one or all 
resources work. Break the interface into smaller specialised ones and move them 
next to the consuming code. In your case you may have an endpoint that uses a 
GetResource method, so you define that interface. Another endpoint may need 
just the GetAllResources method: another interface. You can then test your 
endpoints by supplying a test struct that implements the smaller interface 
needed.


-- Ian

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/a7c46809-bb03-442b-9297-81cb588df72e%40www.fastmail.com.

Reply via email to