Number of Calls Expectation

Create issue
Issue #2 resolved
Cristian Esquivias created an issue

Currently, there is no way to verify that a function has been called once, never or several times.

A new method to Fake should be added to specify how many times a method or callable Fake are called.

For example: {{{ #!python

fake_function = Fake(callable=True).with_arg_count(3).returns(True).num_calls(1)

fake_object = Fake() fake_object.expects('foo').with_arg_count(2).num_calls(3)


Comments (6)

  1. Cristian Esquivias reporter

    Here's a changeset that implements this feature. It includes doctests. Some examples:

    f = Fake(callable=True).times_called(2)
    auth = Fake('auth')
  2. Kumar McMillan repo owner

    Hi. Thanks for the patch! I applied your changes and pushed it to the main repos. Can you try out the changes? You will probably need to backout your changes then grab an update with hg pull -u. I don't think the new changes will merge with yours because I removed the registry.all_calls list. The reason for that is because I think you wanted Fake(expect_call=True) instead of Fake(callable=True). This was a missing feature :) In other words, what you wanted is to expect that the object gets called and *also* expect that it gets called a certain number of times. So what I changed was Fake('login', callable=True).times_called(2) will not raise an error if login() is not called because it is not expected. It will however raise an error if login() is called more than two times.

    See hg help backout for how to remove changesets. Then after updating and installing the new version of the module, let me know if it is working for your test suite(s).

    If all is well, I have one more unrelated fix to make and then I will cut a release. BTW, I added you to a new Credits section in the docs. Thanks again.


  3. Kumar McMillan repo owner

    oh, wait, forget what I said about backing out your changes. Since I applied your bundle, there is no need for hg backout. Just simply do hg pull --update and you might need to merge but everything should be fine.

  4. Kumar McMillan repo owner

    Also take note that I've deprecated two methods and you'll need to update your code (there will be warnings).

    • fudge.start() is now fudge.clear_calls()
    • fudge.stop() is now fudge.verify()
  5. Cristian Esquivias reporter

    Excellent! I like your cleanups. I noticed that I forgot to include my improved error messages, but you took care of that too!

  6. Log in to comment