golang / mock

GoMock is a mocking framework for the Go programming language.
Apache License 2.0
9.32k stars 607 forks source link

Blank output for most files in hashicorp/consul #315

Closed hashcacher closed 5 years ago

hashcacher commented 5 years ago

Go Version: 1.12.1

Consul has a deep nested struct hierarchy that would be delightful to not mock manually. I think I've tried most of the permutations of options and files. Here's the repo: https://github.com/hashicorp/consul/tree/master/api

I tried -source

mockgen -source api/kv.go
mockgen -source api/kv.go -self_package=github.com/hashicorp/consul/api
mockgen -source api/api.go
mockgen -source api/api.go -self_package=github.com/hashicorp/consul/api

All output the same nearly blank file:

// Code generated by MockGen. DO NOT EDIT.
// Source: api/kv.go

// Package mock_api is a generated GoMock package.
package mock_api

import (
    gomock "github.com/golang/mock/gomock"
)

I also tried using reflection:

GO111MODULE=on mockgen github.com/hashicorp/consul/api Catalog,KV,Health,Client

outputs

Reflection: api.Catalog is not an interface
Reflection: api.Catalog is not an interface
go: finding github.com/golang/mock/mockgen/model latest
go: finding github.com/golang/mock/mockgen latest
Reflection: api.Catalog is not an interface
2019/07/16 13:26:10 Loading input failed: exit status 1

I'll also mention that it works great for some other (simpler) files, so it doesn't seem like a setup issue.

Thanks in advance!

balshetzer commented 5 years ago

Hi Greg,

mockgen can only create mocks from Interfaces.

If the system under test takes a struct then it can't be mocked out because the Go type system will only accept that struct as an argument.

If the system under test takes an Interface implemented by your struct then it can take a mock that also implements that Interface.

In other words:

What would you like mockgen to generate from the structs?

Hesky

hashcacher commented 5 years ago

Hey Hesky,

I ran it on another file and mis-read the output. I thought it generated a mock struct and interface but on second glance, it only mocked a struct for the existing interface. Perhaps if I write an interface with all the methods of the consul classes that I need to call, then it could generate mocks for those? In an ideal world, it would automatically write the interface too :)