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

Way to rename constructor functions? #25

Closed
kasvtv opened this issue Apr 4, 2022 · 2 comments · Fixed by #28
Closed

Way to rename constructor functions? #25

kasvtv opened this issue Apr 4, 2022 · 2 comments · Fixed by #28

Comments

@kasvtv
Copy link
Contributor

kasvtv commented Apr 4, 2022

First off, this module is absolutely sublime. Wonderfully convenient for developers to use. Absolutely stellar job creating the developer-facing API! This should be the number one mocking module for Gophers!

The only shortcoming I experience is as follows:

My common workflow with this module, is to generate a bunch of mocks from my interfaces, and then, for some, but not all of the mocks, create a custom wrapper function that first calls the generated constructor function, then assigns a bunch of default returns or default hooks, and return the result. This allows for a super simple way of getting a mock with sensible defaults inside of a unit test (and optionally further change some of the hooks to simulate the scenario that you want to test).

I'd like to keep these custom constructors in the same package as the mock objects, and then apply the NewMock**** function naming convention for my custom constructors (essentially replacing the generated constructor), to signify to other developers that this is the standard mock to use.

What I tried so far:

  • Create the mocks for which I want to use my own custom generator with --prefix=Base as to be able to use the NewMock**** function naming convention for my custom constructors
    • This works, but now all types related to the mock have Base in the name, which is somewhat confusing. Ideally I would just rename the constructor function.
  • Ditch the method of creating my own constructors and instead write them into an interface and use NewMock****From.
    • This works, but is far more verbose, especially when I only want to assign a default hook to one or two functions on an interface defining 20+ functions. It also requires the user of the mocks to specifically call it using the from-function and pass in the interface manually, each time, without being able to simply rely on the NewMock**** convention and get the mock with the sensible defaults.

Any suggestions on how to do this better?

Thanks again for the amazing module.

@efritz
Copy link
Member

efritz commented Apr 4, 2022

Would an additional --ctor-prefix flag that allows you to insert some other name into just the constructor and not all types work for this use-case?

@kasvtv
Copy link
Contributor Author

kasvtv commented Apr 6, 2022

Would an additional --ctor-prefix flag that allows you to insert some other name into just the constructor and not all types work for this use-case?

Yes, certainly!!

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

Successfully merging a pull request may close this issue.

2 participants