3

I am working with Golang and using mockhiato to generate mocks for all interfaces. This tool generates mocked implementation in mocks.go file within the same package. I can't rename mocks.go to mocks_test.go as this mock file is consumed by other packages.

The problem is that these mocks files are counted by go coverage tool and thus reducing my code coverage percentage for the package.

I am looking a good workaround so that my code coverage will not show bad numbers.

Rahul Garg
  • 4,069
  • 1
  • 34
  • 31
  • 2
    Out of curiosity, why are your mocks consumed outside of tests? – fstanis Jan 27 '18 at 22:15
  • We are following DDD and in DDD usually, domain expose its repository interface and this interface implemented by repository later. Similar for services, which consumed by handler layer. – Rahul Garg Jan 28 '18 at 07:03
  • Mocks consumed by test code itself but outside of that package (Golang specific) – Rahul Garg Jan 28 '18 at 07:50
  • 1
    Shared mocks should live in a separate package. Then only your other `*_test.go` files need to depend on them. – Jonathan Hall Jan 28 '18 at 12:01

2 Answers2

2

The best thing in this case would be to move the mocks to their own dedicated package which would have no test coverage. This would remove their impact on code that you actually want coverage data on.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
0

That's how we solved it.

  1. Put interface in the consumer folders. If service is injected in the handler, then the handler will have the interface definition of service. This is because GoLang philosophy suggests that interface is to consume functionality rather than expose it.
  2. Used mockery to generate mocks.

  3. Generate mocks in a separate _mock folder.

Rahul Garg
  • 4,069
  • 1
  • 34
  • 31