Check if method was called only once

Issue #2 wontfix
stefanwehner created an issue

Hi Carlos,

We want to assert that a method was only called once, so we tried to specify something like that:

assert_that_method(notifier.notify).was_called().times(1)

This doesn't work and the framework suggests times should be at least 2. If we do not specify times(), then the assert passes if the method was called AT LEAST ONCE - not just one time. So it seems like there is no way to require that a method is only called once.

We looked a bit at the code, and we'd suggest the attached diff. We hope this helps, Best regards,

Stefan Wehner & Eduardo Ferro

PD: Por cierto - muy util la libreria ;-)

Comments (6)

  1. Carlos Ble repo owner

    Hi guys! Thanks for your feedback and your code. I have a question for you. If you have to make sure that just one call is made, you probably need a mock, not a spy. Have you thought that? The mock will fail if any unexpected call happens. Let me know if a mock is enough for you :-)

    Cheers

  2. stefanwehner reporter

    (Reply via ste...@alea-soluciones.com):

    Hi Carlos,

    First of all thanks for the quick response and yes... you're right - we haven't seen that using a mock we can enforce that the method is called only once.

    The only drawback is that we have to write expect_calls for every other method that is called on the mock object. This makes our test a bit more bloated and fragile.

    What we thought was a bit counterintuitive of the spy's was_called.times(X) semantics is that you can assert that the method was called:

    • 0 times - was_never_called()
    • one or more times - was_called()
    • exactly n times (with n>=2) - was_called().times(n)

    So maybe it would be worthwhile to include this behaviour? Let us know what you think,

    Thanks for the help, Kind regards,

    Stefan & Eduardo

  3. David Villa Alises

    Hi guys:

    Doublex supports the feature stefanwehner asks using hamcrest matchers. You can see some examples in [1].

    Note that the doublex "times(2)" means "equals to 2" but pyDoubles "times(2)" means "at least 2". This implies that it is not possible to directly provide support for the same feature in pyDoubles through the wrapper due there is a semantic difference. I recommend you to use doublex instead of pyDoubles if you require that feature.

    Thanks for your feedback.

    [1] https://bitbucket.org/DavidVilla/python-doublex/src/61f898866c0c/test/unit.py#cl-488

  4. Log in to comment