-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add .Unset method to mock #982
Conversation
I'm not sure about the pattern used here. It seems less clear & implicit. Would it not be clearer if you attached the Seems like the kind of situation where a bit of repetition is better than abstraction. :) If the community feels strongly about this, I'd be happy to have another look. |
@boyan-soubachov it forces all the logic to be consolidated in one Setup function if you can't override / remove individual mock handlers OR you can duplicate logic across tests. Use case: SetupJobTest (run before each test)
TestInvalidJobTransfer
I think providing different options allows more flexibility for different types of use cases. LMK if what I said makes sense. |
Another option would be if you could prepend handlers to the list that would allow you to override handlers created before. That's probably the crux of the issue and having |
Just had some more thoughts, I don't really have to call That would be a quick and easy change though, just changing this to do a prepend https://github.com/stretchr/testify/blob/master/mock/mock.go#L274. |
@pdufour for your particular use case wouldn't it be easier to create a func createStub() mock.Mock {
m := MyMock{}
m.On("CommonMethod1").Returns(...)
m.On("CommonMethod2").Returns(...)
}
func createRelaxedStub() mock.Mock {
m := MyMock{}
m.On("CommonMethod1").Returns(...)
m.On("CommonMethod2").Returns(...)
m.On("CommonMethod3").Returns(...)
}
func TestFoo(t testing.T) {
m := createStub()
m.On("SpecificMethod").Returns(...)
} In Go it seems most projects either wrap complex Mocks within nice interfaces or create the objects per test (which would also be my preference in majority of the cases) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
On of the issues that this PR would solve is if your unit under test has a few dependencies and you want to set them all up at the beginning in setup. func (s *ATestSuite) SetupTest() {
s.dep1 = &mocks.Dep1{}
s.dep2 = &mocks.Dep2{}
s.dep3 = &mocks.Dep3{}
s.uut = NewUnitUnderTest(s.Dep1, s.Dep2, s.Dep3)
s.dep1.On("CommonCall1", mock.Anything, mock.Anything).Return(false, nil).Maybe()
s.dep2.On("CommonCall2", mock.Anything, mock.Anything).Return(false, nil).Maybe()
s.dep3.On("CommonCall3", mock.Anything, mock.Anything).Return(false, nil).Maybe()
} Then in each test I want to focus on the interactions with only one dependency. I don't want to have to setup the UUT at the beginning of each test; I want it int the setup function. If I can just override one of these default stubs, then I can focus on what is unique to this one test. If I cannot, then I'm forced to repeat the setup logic in each of my tests. |
I just ran into a scenario where I wanted this feature. Sad to see that it has not been merged into the project yet. Please implemented it soon as it would be a great help |
+1, I recently wrote a very large test suite where this would've been very useful. |
Same here, it would extremely helpful if you were to merge this feature (or a newer version of it, if needs) |
Excuse me, I've been quite busy. Given that the e.g. IMO that would be more consistent and intuitive when factoring in the use case context. Whilst "Off" makes sense as being the opposite of "On", it doesn't seem to make sense in this context since the word "On" being used isn't the verb but rather the adverb. |
@boyan-soubachov I agree with you that the method name suggested isn't consistent with the other method names of the mocks but the main issue of removing or clearing
|
Agreed, the feedback seems to suggest it would be a useful feature for the community; contrary to my earlier comment :)
Fair enough, we could have something like Maybe even a |
That sounds great to me! Wondering when we can get this feature merged? Thanks! |
Is there anything needed yet that's blocking this? We have to use |
Any update on this? Looking for such a feature. Thanks |
I would also appreciate this feature |
I'm just going to close this since it's just a bunch of +1,s the author of the repo provided an API he'd be happy with, so if you'd like the feature, go ahead and develop it and make a PR. |
@pdufour , no need to close it, IMO. We're just debating the naming :) Would you be happy with renaming the |
@boyan-soubachov I no longer use mockery but that seems reasonable to me. |
@boyan-soubachov I renamed it as Unset but I'm not sure if you were looking for the different API still or just the rename? |
@boyan-soubachov Went ahead and updated the API, PTAL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Thank you for your contribution :)
Summary
This adds the ability to remove handlers that were added with
.On
Changes
.Unset
MethodMotivation
Nice to have this in tests where test will share majority of same mock handlers expect you might want to remove just 1 handler in one test.
Related issues
#558