vektra / mockery

A mock code autogenerator for Go
https://vektra.github.io/mockery/
BSD 3-Clause "New" or "Revised" License
6.11k stars 410 forks source link

Is it possible to mock a class from the standard library? #19

Closed miloconway closed 8 years ago

miloconway commented 9 years ago

For example, want to generate a mock for "net/http" RoundTripper

ernesto-jimenez commented 9 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
}
evanphx commented 9 years ago

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.

miloconway commented 9 years ago

I see, thank you for the clarification

wolfgangmeyers commented 8 years ago

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.

ernesto-jimenez commented 8 years ago

@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

evanphx commented 8 years ago

@ernesto-jimenez Would you be willing to merge mockery and goautomock? Combining forces might be good!

wolfgangmeyers commented 8 years ago

+1

ernesto-jimenez commented 8 years ago

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

evanphx commented 8 years ago

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?

ernesto-jimenez commented 8 years ago

Opened an issue in testify: https://github.com/stretchr/testify/issues/273

evanphx commented 8 years ago

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.

mikegleasonjr commented 5 years ago

any news on this..

mikegleasonjr commented 2 weeks ago

The pattern here is to do something like this:

type writer interface {
    io.Writer
}

And use mockery to generate writer

LandonTClipp commented 2 weeks ago

No you can just mock io.Writer directly:

packages:
  io:
    interfaces:
      Writer:
mikegleasonjr commented 2 weeks ago

No you can just mock io.Writer directly:

packages:
  io:
    interfaces:
      Writer:

Is there a command line equivalent?

LandonTClipp commented 2 weeks ago

There is not, you have to use the packages feature.