Giter Site home page Giter Site logo

Comments (6)

kaste avatar kaste commented on June 28, 2024

I have something like this in the code: Look out for self.allow_zero_invocations whose value depends on

def verification_has_lower_bound_of_zero(verification):
if (
isinstance(verification, verificationModule.Times)
and verification.wanted_count == 0
):
return True
if (
isinstance(verification, verificationModule.Between)
and verification.wanted_from == 0
):
return True
return False

iirc when times=0 or between starts at 0, i.e. between=(0,3), the stubbed method counts always as used.

from mockito-python.

kaste avatar kaste commented on June 28, 2024

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.

Oracking avatar Oracking commented on June 28, 2024

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.

kaste avatar kaste commented on June 28, 2024

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.

Oracking avatar Oracking commented on June 28, 2024

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.

kaste avatar kaste commented on June 28, 2024

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:

class TestPassIfImplicitlyVerifiedViaExpect:
@pytest.mark.parametrize('verification', [
{'times': 0},
{'between': [0, 3]}
])
def testPassIfImplicitlyVerified(self, verification):
dog = mock()
expect(dog, **verification).waggle().thenReturn('Sure')
verifyStubbedInvocationsAreUsed(dog)

from mockito-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.