-
Notifications
You must be signed in to change notification settings - Fork 5
Be consistent when mocking functions & properties #11
Conversation
I don't know about this. My question revolves around why you need to mock a function at the class level. In OOP, everything should be an instance. Class level functions should be rare. |
@tizz98 I think this is adding complexity to something that shouldn't be complex. The only reason we use As Brett said, the cases where you need to mock classes' attributes should be (and are) rare. Today it's restricted to static/class methods (which, as far as I know, we're trying to remove), third party classes/objects and QuerySets, as well as custom QuerySet methods. |
If we should only mock instance attributes, and there is no reason to mock instance attributes using |
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.
I agree with @tizz98's proposal, for the sake of consistency. Mocking instance members isn't wrong, but it looks similar to mocking class members and that alone can help developers make copy-paste mistakes.
patch
andpatch.object
are always safe and easy to follow and control.- Explicit using
Mock
(and its children) looks more consistent for creating things instead of overriding them.
I also do @emyller, and that's because we'll have two different ways to mock properties (class and instance attributes) inside our code. Since we should never assign a class attribute because it will change the same class for all the following tests, we need to ensure all changes are being scoped. |
Looking at the I agree that people may do it wrong, though. That's why the reviews led by Brett are so strict. Maybe making it more explicit in the doc should fix this possibility? |
@jourdanrodrigues My say is: the |
@emyller I'm not talking about what it is for, it patch things very well, which is why we use it. What I'm talking about is its use-cases. I don't know what you meant with "patch things manually", because we still have to type the mock with the Again, we're using But since I'm the only one here that don't agree with this, I'll be less passionate about it and just quote Brett: if it is fast (less than 0.1s as off now), go for it. |
Why didn't we merge this? |
Proposed: #6 (comment)