I've been most recently experimenting with GoMock
, the test mocking framework supported by the official creators of the Go language. I was wondering where is the most reasonable place to put these mocked files.
My current directory structure is as follows. Is this how Go projects should be structured?
appname
|-- gateways
|-- gateway1.go
|-- gateway1_test.go
|-- gateway2.go
|-- gateway2_test.go
|-- mocks
|-- gateway1.go
|-- gateway2.go
This is slightly influenced by Ben Johnson's talk here.
I tend to follow this article also by Ben Johnson
Link to article
In general this approach of having a package for your shared mocks is a good one. One thing worth pointing out here is that if you don't define your models outside of the gateways
package you could get an import cycle.
gateways
defines models mocks
imports gateways.Model
gateways
imports mocks
for testsThere are 2 ways to fix this; the first is just to move your models into domain
, a root package outside gateways
(see article for examples). Or only test the public interface of your gateways
package by using gateways_test
as the package in your test files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With