Closed miloconway closed 8 years ago
Hey @miloconway,
mockery seems prepared to create mocks for your interfaces rather than from the standard library. However, you can checkout the Go repo, generate the mock, and copy it to your project.
Here's one generated for RoundTripper
import "github.com/stretchr/testify/mock"
type MockRoundTripper struct {
mock.Mock
}
func (m *MockRoundTripper) RoundTrip(_a0 *Request) (*Response, error) {
ret := m.Called(_a0)
var r0 *Response
if ret.Get(0) != nil {
r0 = ret.Get(0).(*Response)
}
r1 := ret.Error(1)
return r0, r1
}
mockery could be setup to parse other packages to generate mocks, but if you want to mock an interface usually you want some control over it and thus copying the interface into your package is better way to handle that.
I see, thank you for the clarification
I'd like to be able to generate mocks for other libraries. Copying interfaces into my own code seems like a reasonable workaround, but it would be nice if this were a first-class feature.
Example annoyance:
$ grep -r "type ResponseWriter interface" /usr/local/go/src/
/usr/local/go/src//net/http/server.go:type ResponseWriter interface {
Not hard, but kind of annoying to track down.
@wolfgangmeyers, I've got to spend some time this weekend reviewing and documenting the tool, but you can go get github.com/ernesto-jimenez/gogen/cmd/goautomock
and goautomock
should let you generate mocks from the stdlib.
We switched from mockery
to goautomock
to address the issues mockery had that could be fixed by requiring Go 1.5
@ernesto-jimenez Would you be willing to merge mockery and goautomock? Combining forces might be good!
+1
@evanphx, that was my initial approach from issue #41, but you mentioned you didn't want to require Go 1.5 :)
I'm maintaining testify
now, and I've been thinking wether the tool should be part of the package.
It might be worth while converging into testify itself. I'm the maintainer and never create mocks by hand.
Ah! I'd love to merge mockery into testify. I think that Go 1.5 has probably been around long enough now that it's ok to require it.
How can we make this happen?
Opened an issue in testify: https://github.com/stretchr/testify/issues/273
I'm updating mockery to use go/types
and once that is merged, you'll be able to do the same as #18 to make this work, namely make an interface in your package that imports the interface in the other package.
any news on this..
The pattern here is to do something like this:
type writer interface {
io.Writer
}
And use mockery
to generate writer
No you can just mock io.Writer
directly:
packages:
io:
interfaces:
Writer:
No you can just mock
io.Writer
directly:packages: io: interfaces: Writer:
Is there a command line equivalent?
There is not, you have to use the packages feature.
For example, want to generate a mock for "net/http" RoundTripper