-
Notifications
You must be signed in to change notification settings - Fork 420
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Design Proposal: integration of github.com/matryer/moq
-style mocks
#712
Comments
github.com/matryer/moq
-style mocksgithub.com/matryer/moq
-style mocks
Hi @LandonTClipp
As a experienced user of
and
I think this critic is debatable as well. As long as there is no need for just an other config file, that pollutes my repository, since everything that is needed to generate the mocks fits into the
In regards to the possible values for the configuration attribute
I wonder, if the generator could be made "generic" in the sense that it just uses Go templates again to generate the final result. Similar to how it is done in https://github.com/hexdigest/gowrap. Basically mockery would just collect the necessary information (state), that is needed to generate the mocks and pass this to the Generator, which then basically loads a Go template where the final result is generated based on the state that is passed. I hope, this thoughts are helpful for you. |
I myself am not super familiar with moq so it's great to have you in this discussion :D
I was a bit confused as to why this would be a "challenge," but what you suggested (either an external counter or a queue of return values) is what I thought would be done. In fact, the reason why I like that method more is precisely because it's ugly/manual. I've found it to generally be (like 80% of the time) bad practice, so I feel the mock framework should discourage it by default due to the increased coupling it creates. The main criticisms that have been raised is that
I agree, on the surface this is a matter of taste. I prefer centralized config files, some people prefer
This had been suggested, but I rejected the idea on the grounds that it didn't seem like a highly requested feature and there are ways around the lack of inheritable config files. So as it stands, the only inheritance is your typical 3 sources of environment, CLI, and config files (and of course the mappings within the config file itself).
You are probably right. Maybe:
That's a great idea. We could probably structure a new kind of generator that does just that. Unfortunately it would be very hard to port over the current |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
matryer/moq#208 was submitted to the maintainers of matryer/moq suggesting integrating the moq-style mocks into mockery. This suggestion seems well-received due to the benefits we could gain from combining the two projects' strengths.
This GitHub issue serves as a design proposal, discussion, and feature tracking page.
Background
vektra/mockery
style of mocks rely on argument matching behavior provided fromtestify/mock
. Expectations are created by mapping argument values to return values and side effects. Many valid criticisms have been raised about various pitfalls of this kind of expectation system. There are various alternate strategies for defining mock objects, and it's the goal of this project to combine the strengths of these alternate strategies with the strengths implemented in mockery itself.mockery benefits
Speed
Mockery has undergone a huge rework over the last year with the new
packages
feature. Historically, mockery (and basically all other mock implementations) relied heavily on usinggo:generate
statements to generate one mock per instantiation ofmockery
. This was extremely inefficient because it meant thepackages.Load
call required to parse the AST had to parse the package and all of its dependencies many times. This was anO(n*m)
operation (wheren
is the number of mocks being generated, andm
is the number of dependent packages). The rather trivial insight was to simply collect all packages to parse within a single list and pass all of them intopackages.Load
at once, which removedm
from the equation. The new configuration model allows mockery to have this singular list of packages available within a single process.Configuration inheritance
The other benefit of the configuration model is that allows inheritable configuration. Configuration can be specified at the top-level defaults, at the package level, and at the interface level, which parameters being inherited at each level. This is a powerful way to differentiate mock generation based on individual needs.
Templating
The configuration also takes advantage of Go templating, which allows users to call upon variables that are only known at runtime. Templating functions provide more power and flexibility to users to alter template variables.
mockery criticisms
mock.Anything
in mock objects means that the expecter's argument types have to beany
. This means that argument types are not a compile-time safety guarantee but rather runtime.testify
that goes into correctly mapping method calls to their corresponding expectations. While testify does its job perfectly well, its clear that the complicated details of argument-matching necessitates a huge amount of logic to support it. A different mocking model could greatly simplify the generated code.matryer/moq
benefitsSimplicity
These style of mocks provide an incredibly simple way of defining mocks: you define the function to be run for your mock call. No argument matching, no assertions on how the mock was called. This simplicity encourages the programmer to not do too much with the mock, which reduces coupling. It doesn't make it impossible to do nasty things like call count and argument value assertions, but it makes it somewhat harder to do.
Type safety
moq
objects are strictly type safe, which means test compilation will fail immediately if your mock doesn't satisfy the interface no matter what.matryer/moq
criticismsSpeed
Like all other mock projects besides mockery,
moq
relies ongo:generate
to instantiate onemoq
process per mock object. This is highly inefficient for the reasons aforementioned.Configuration model
Its configuration model is entirely CLI parameter based. There are no config files, no inheritable config, no use of Go templating.
Proposal
Configuration syntax
moq-style mocks can be easily called upon in mockery by yaml such as this:
Of course,
style
could be specified anywhere: at the top-level (defaults), in an environment var, in the CLI parameter, at the package level, or at the interface level.Generated code
To maintain backwards compatibility with all existing usage of
moq
, the generated code would be identical to that ofgithub.com/matryer/moq
, minus any necessary differences to comments within the mock file. For those unfamiliar, this provides an example of the mockmoq
generates.Given:
This is generated.
You then define each function individually in your test as such:
File placement
Where the mock files are physically placed will be according to the
dir
parameter, just as in the current system.Code implementation
mockery has a concept of an
Outputter
, which is the object that determines where a mock should physically reside, and aGenerator
which creates the mock code. TheOutputter
should be modified to take an interface value in its constructor:Both mockery's current implementation, and
moq
, would implement this interface. The outputter will be provided the proper implementation here based off thestyle
specified.packages.Load
changesmoq currently calls
packages.Load
based off filesystem path: https://github.com/matryer/moq/blob/c59089b2cd89d975f3d44924f04f37591fbd701d/internal/registry/registry.go#L31This will need to be modified to integrate with the
Interface
struct that contains the AST information about each interface discovered in the package. This struct is generated using the information returned from here.The text was updated successfully, but these errors were encountered: