Thanks for the response Ian. My apologies for a delayed response (inline). On Friday, December 13, 2019 at 6:11:20 AM UTC-8, Ian Davis wrote: > > On Thu, 12 Dec 2019, at 10:27 PM, karth...@gmail.com <javascript:> wrote: > > Specifically, I couldn't find how to partially mock the struct. Most > suggestions revolve around breaking such structs 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. > This is a bit counterintuitive. Maybe this is how golang expects developers to write, but it is generally preferable to think of an interface as a contract that must be satisfied by any struct implementing it. For e.g., io.Writer interface expects any "writable" to implement the "Write" method and so, can be passed to any method that expects an io.Writer. This is pretty much I am trying to do. I want any struct to be able to implement the ResourceManager interface and thus can be passed to any method that accepts the ResourceManager interface.
> First write your tests against the struct to verify that getting one or > all resources work. > Right here, is my problem. Let's assume that I do write such a struct. However, it is not obvious to me how I can test GetAllResources when it internally invokes GetResource. One solution that I found was to have package level functions get and getAll and when mocking getAll, replace the get function with a mock. This is still a bit kludgy, but for now, I don't see a better alternative. > 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. > I think the solution you propose above is for a problem that is different that I asked. My question was how to test the ResourceManagerImpl struct. > > > -- 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/731c8690-2ffe-4093-83b2-b2b95eff425f%40googlegroups.com.