Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
miloconway opened this issue May 13, 2015 · 16 comments
Closed

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

miloconway opened this issue May 13, 2015 · 16 comments

Comments

@miloconway
Copy link

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

@ernesto-jimenez
Copy link
Contributor

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
Copy link
Member

evanphx commented May 18, 2015

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
Copy link
Author

I see, thank you for the clarification

@wolfgangmeyers
Copy link

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
Copy link
Contributor

@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
Copy link
Member

evanphx commented Feb 12, 2016

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

@wolfgangmeyers
Copy link

+1

@ernesto-jimenez
Copy link
Contributor

@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
Copy link
Member

evanphx commented Feb 12, 2016

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
Copy link
Contributor

Opened an issue in testify: stretchr/testify#273

@evanphx
Copy link
Member

evanphx commented Apr 6, 2016

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.

@evanphx evanphx closed this as completed Apr 6, 2016
@mikegleasonjr
Copy link

any news on this..

@mikegleasonjr
Copy link

The pattern here is to do something like this:

type writer interface {
	io.Writer
}

And use mockery to generate writer

@LandonTClipp
Copy link
Collaborator

No you can just mock io.Writer directly:

packages:
  io:
    interfaces:
      Writer:

@mikegleasonjr
Copy link

No you can just mock io.Writer directly:

packages:
  io:
    interfaces:
      Writer:

Is there a command line equivalent?

@LandonTClipp
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants