Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which directory to put mocks?

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.

like image 402
hlin117 Avatar asked Oct 17 '22 16:10

hlin117


1 Answers

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.

like image 106
Zak Avatar answered Oct 21 '22 05:10

Zak