moq icon indicating copy to clipboard operation
moq copied to clipboard

Support for mocking core types

Open gr8routdoors opened this issue 2 years ago • 8 comments

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.

gr8routdoors avatar Feb 27 '23 23:02 gr8routdoors

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

matryer avatar Jun 21 '23 10:06 matryer

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!

tanoinc avatar Jul 21 '23 09:07 tanoinc

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 avatar Jul 21 '23 10:07 matryer

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.

matryer avatar Jul 21 '23 10:07 matryer

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

Thanks Mat!

tanoinc avatar Jul 21 '23 10:07 tanoinc

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 avatar Nov 27 '23 01:11 msbit

Or:

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

msbit avatar Nov 27 '23 01:11 msbit

You might also consider using a type alias or simply copying the interface to your code, as discussed in https://github.com/matryer/moq/issues/172#issuecomment-1084369939

breml avatar Nov 27 '23 15:11 breml