stretchr / testify

A toolkit with common assertions and mocks that plays nicely with the standard library
MIT License
23.02k stars 1.58k forks source link

Please make a public generator for testify mocks akin to https://github.com/matryer/moq #728

Open scr-oath opened 5 years ago

scr-oath commented 5 years ago

https://github.com/matryer/moq generates a mock from an interface which, though nice, isn't as rich a mocking language as testify (ability to just say how many times, or return what as part of test).

It would be great if testify could provide a means of generating, for instance, from:

type MyInterface interface {
  DoSomething(int) (int, error)
}

this:

type MyInterfaceMock struct {
  mock.Mock
}

func (m *MyInterfaceMock) DoSomething(in1 int) (int, error) {
    replyArgs := m.Called(in1)
    return replyArgs.Int(0), replyArgs.Error(1)
}
HaywardMorihara commented 5 years ago

I agree, would be lovely to have it right in testify

But, until that's a reality, you could always try https://github.com/vektra/mockery. I've used it with success so far

scr-oath commented 5 years ago

Maybe just a link to that from testify mock package godoc would do the trick?

-SCR

Sent from my iPhone

On Feb 15, 2019, at 9:40 PM, Nathaniel Morihara notifications@github.com wrote:

I agree, would be lovely to have it right in testify

but, until that's a reality, you could always try https://github.com/vektra/mockery. I've used it with success so far

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/stretchr/testify/issues/728#issuecomment-464295272, or mute the thread https://github.com/notifications/unsubscribe-auth/An-w7d5ytP48VK_xU7em34lpJNKccaBbks5vN5nhgaJpZM4a-WQD .

sagikazarmark commented 5 years ago

Unfortunately vektra/mockery does not seem to be maintained. I agree it would be somewhat better if this package provided a solution OOTB.

alexejk commented 4 years ago

I am forced to remove vektra/mockery in most places as it seems to be causing quite a bit of issues (OOMs in containers etc) and requires to vendor dependencies to mock 3rd-party packages. Luckily in my case it's fairly easy to hand-mock things.

Would love to have an out of the box solution for it that also works with modules.

sagikazarmark commented 4 years ago

I've built a tool for generating code and added support for generating testify mocks:

https://github.com/sagikazarmark/mga#testify-mock-generator

It works well with modules as well.

I'm working on better documentation for the project, but the tool already works and generates mocks in a real life project.