-
Notifications
You must be signed in to change notification settings - Fork 412
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
Allow for No Return Value on Generated Mocks #796
Comments
+1 on this I also think it's beneficial to use mock1.On("Method").Return(fakeStruct)
mock2.On("Method", fakeStruct).Return(anotherSomething) In this case, I don't care about what is returned by mock1.On("Method").Return(mock.Anything)
mock2.On("Method", mock.Anything).Return(anotherSomething) |
I think there is a fundamental misunderstanding here of what mocks are. Let's go to your function: func (s *Service) One(ctx context.Context) error {
if err := s.storage.BeginTxFunc(ctx, func(pgx.Tx) error{
if err := s.storage.Two(ctx, tx); err != nil {
return err
}
if err := s.storage.Three(ctx, tx); err != nil {
return err
}
}); err != nil {
return err
}
} You have mocked out If you wanted the anonymous function to be run, you have to use something like
This is indeed not what's happening, as shown above. As to the broader question of allowing us to not specify a return value, that's unfortunately not something I'm interested in doing. This has been asked before but it creates a further maintenance burden on me to ensure that mockery knows how to always create a zero-value return if nothing was specified (and from a code generation perspective, it's actually not easy to do that for all cases generally). While technically possible, I don't see a real benefit to it. There is a lot of value in being as explicit as possible, and explicitly telling the mock you want it to return a zero-value for your return type is a good thing IMO. |
What you're asking for is far more complicated than you might think. How is mockery supposed to know what kind of struct it should create for your returned interface? Should it be an empty struct? A stub? If you're returning an interface, why not just create a mock for it and return the mock? |
While it's possible to mock the first return, doing so adds unnecessary complexity to the test cases.
The |
Verification of the return value doesn't happen anyway. If your mocked method gets called, mockery has to return something. It has no option. So what does it return? Well, it can return a zero-value, but what does a zero-value look like for an interface? What does that mean? The point is, this is not easy to do for the general case. |
Description
Sometimes it is necessary for mocked functions to allow for not checking return values. I am currently running into an issue with mocks generated via mockery where I have to specify a
.Return
value for a mock even though the return value of that mock makes no difference to the running of the functions within the mock.Mockery Version
mockery v2.43.2
Golang Version
go 1.22.5
Installation Method
Steps to Reproduce
BeginTxFunc
being used before either of the other two functions can be calledAs you can see, the forcing of a
.Return
value onm.On("BeginTxFunc")
stops the test from calling the other two functions as they are nested and the function has already returned.I have gotten around this problem before by not definining a
.Return
value when mocking theBeginTxFunc
function, but this appears to not be possible via mockeryThe line causing me problems specifically in the generated mocks is this
Expected Behavior
An option to allow mocks without
.Return
valuesOR
An option to override the mock generation for a specific function. For this situation I have found that this works
Actual Behavior
panic: no return value specified for Xyz
OR
FAIL: 1 out of N expectation(s) were met.
The text was updated successfully, but these errors were encountered: