Comments (6)
I have something like this in the code: Look out for self.allow_zero_invocations
whose value depends on
mockito-python/mockito/invocation.py
Lines 288 to 301 in 766ea5c
iirc when times=0
or between starts at 0, i.e. between=(0,3)
, the stubbed method counts always as used.
from mockito-python.
I forgot to ask
This is useful in a case where we want to always mock some call in the beforeEach regardless of whether it is called in the test body (see example below).
I see the example but why do we do this? Why stubbing when you don't use it? Is that just dev ergonomics?
from mockito-python.
Oh great! Just checked and between
can be used to achieve the desired behaviour. I had tried that previously but it did not work for a different reason (it had escaped me since I'm raising this issue a few days later).
To your question - "why do we do this?" - an example scenario will be:
Say I have a TestClass that has some test cases that test function_a. Inside function_a, it conditionally invokes function_b that sends an alert. And I want to guarantee that the real alert call is never made in a test context. In this case, it feels safer to plug the mock for function_b in the setUp method. But this means some test cases that call function_a will not necessarily invoke the mocked function_b. However, it is safer because anyone who extends the test class with new test cases doesn't have to remember to mock the external service call when developing, risking unintended alerts.
Also, the reason that between
had not worked for me was because expect
does not work when we pass the module name as a string. I see when
resolves the module it receives using get_obj. However, the same is not being done for expect
. Is this intentional?
from mockito-python.
Can't see anything why expect
should be different here. Mistake? Usually just change it and see if some tests are failing. (I so rarely do the strings thing in python, I think.)
from mockito-python.
Understood. I will make the change and check if the tests run fine. If all good I will raise a PR and close issue. Appreciate the quick responses :)
from mockito-python.
When we do something with the docs, it should be added to the docs of verifyStubbedInvocationsAreUsed
. That alone makes it a lot more "findable". It also makes it clear where the tests for this should be located. (Okay, tests locations are wild in this repo. LOL)
Edit: We have a test for that exact use-case:
mockito-python/tests/instancemethods_test.py
Lines 303 to 313 in 22e0451
from mockito-python.
Related Issues (20)
- resetting a mock? HOT 1
- Add support for capturing all the arguments when there are multiple invocations HOT 15
- Add a method `thenCallRealMethod` to AnswerSelector to call the original method HOT 3
- ArgumentError: obj is not registered HOT 4
- Getting the mocked obj instance in the mock method HOT 6
- captor is capturing even when the whole signature doesn't match. HOT 2
- getting the valid input in mock HOT 58
- Issue unstubbing random.randint HOT 2
- Let mockito check expectations on `__exit__` HOT 1
- Mockito does not unstub inherited methods HOT 3
- no attributes in dummy objects since 1.3.2 HOT 4
- Mypy integration HOT 3
- No longer working with Python 2.7 HOT 7
- new bug introduced/functionality removed in upgrade from 1.3 to 1.4 HOT 5
- How to verify stubbed methods (from different mocks) are called in order? HOT 5
- Feature request: add a `.thenDoNothing()` method to `AnswerSelector`? HOT 8
- "Called but not expected" with mocked open() HOT 1
- How to use matchers/captor? HOT 2
- Feature request: Mock current date/time HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mockito-python.