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

Support for mocking core types #191

Open
gr8routdoors opened this issue Feb 27, 2023 · 8 comments
Open

Support for mocking core types #191

gr8routdoors opened this issue Feb 27, 2023 · 8 comments

Comments

@gr8routdoors
Copy link

Have you considered adding the ability to mock built-in golang types? It seems like a mixed bag out there of libraries and people hand rolling various core type mocks. Seems like this might be useful and potentially doable through additions to the registry loading logic.

@matryer
Copy link
Owner

matryer commented Jun 21, 2023

@gr8routdoors what use cases do you have for this? Would it be mocking interfaces only?

@tanoinc
Copy link

tanoinc commented Jul 21, 2023

Hey, thanks for all the work you're doing, this project is great!

Regarding this issue, I have a similar use case, for example I'm using a RoundTripper implementation as a dependency, for a Http client which can hold some middlewares. For the unit tests, I need to mock an interface from standard lib (net/http holds the RoundTripper interface).

If there's a way to do it (that I don't know), please excuse me 😅

Thanks again!

@matryer
Copy link
Owner

matryer commented Jul 21, 2023

Honestly, it might be easier to write a quick RoundTripper function in your test code, and use that - it can sometimes be simpler.

This doesn't negate the discussion around this feature though. Just trying to understand it a bit more.

@matryer
Copy link
Owner

matryer commented Jul 21, 2023

I can see the argument to have them look/feel the same as the other mocks though. So yeah, this feels more and more like something we should support.

@tanoinc
Copy link

tanoinc commented Jul 21, 2023

True, for this simple specific case, I ended up creating a mock manually. But I could imagine other scenarios.

Thanks Mat!

@msbit
Copy link

msbit commented Nov 27, 2023

Without any real testing, it looks like this works:

moq /opt/local/lib/go/src/net/http RoundTripper

but it would be nice to have an install agnostic way to spell /opt/local/lib/go/src

@msbit
Copy link

msbit commented Nov 27, 2023

Or:

moq $(go env GOROOT)/src/net/http RoundTripper

@breml
Copy link
Contributor

breml commented Nov 27, 2023

You might also consider using a type alias or simply copying the interface to your code, as discussed in #172 (comment)

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

5 participants