8

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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
hlin117
  • 20,764
  • 31
  • 72
  • 93

2 Answers2

4

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.

  1. gateways defines models
  2. mocks imports gateways.Model
  3. gateways imports mocks for tests

There 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.

Zak
  • 5,515
  • 21
  • 33
0

I place it in the same package as my interface and in my opinion it's a good place because it's separated from inherited structs I think your project structure is not good I rather do below (its base on DDD architecture)

example

appname
|-- gateways
    |-- gateway.go //this file contain interface and mocks 
    |-- gateway1
        |-- gateway1.go
        |-- gateway1_test.go 
    |-- gateway2
        |-- gateway2.go
        |-- gateway2_test.go 
Mahdi mehrabi
  • 1,486
  • 2
  • 18
  • 21