Mock does not have assert_called_once method

Bug #1394544 reported by git-harry
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Cinder
Fix Released
Low
git-harry

Bug Description

$ grep -rEl 'assert_called_once\(' cinder
cinder/tests/keymgr/test_barbican.py
cinder/tests/test_backup_driver_base.py
cinder/tests/test_hp_msa.py
cinder/tests/test_rbd.py
cinder/tests/test_smbfs.py
cinder/tests/test_utils.py
cinder/tests/windows/test_vhdutils.py
cinder/tests/zonemanager/test_brcd_fc_zone_client_cli.py
cinder/tests/zonemanager/test_fc_zone_manager.py

This files all contain mocks that are having the method assert_called_once called. This doesn't work as expected because it is not one of Mock's assertion methods. The method that should be called is assert_called_once_with.

Nova has a hacking check for this because it is a common mistake.

Tags: tests
git-harry (git-harry)
Changed in cinder:
assignee: nobody → git-harry (git-harry)
Mike Perez (thingee)
Changed in cinder:
importance: Undecided → Low
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to cinder (master)

Fix proposed to branch: master
Review: https://review.openstack.org/136756

Changed in cinder:
status: New → In Progress
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to cinder (master)

Reviewed: https://review.openstack.org/136756
Committed: https://git.openstack.org/cgit/openstack/cinder/commit/?id=322126212e44358446c0fcabfa451a2f2b76f146
Submitter: Jenkins
Branch: master

commit 322126212e44358446c0fcabfa451a2f2b76f146
Author: git-harry <email address hidden>
Date: Sat Nov 22 10:16:22 2014 +0000

    Fix calls to assert_called_once in unit tests

    Mock has a method called assert_called_once_with to check that a mock
    was called and the arguments it took were as expected. Mock does not
    have a method called assert_called_once and calling it just creates a
    mock bound to that name. This means that not only is nothing tested
    when assert_called_once is used, the tests also don't warn about this.

    This commit attempts to address this in two ways:
        - all occurrences of assert_called_once are replaced with a real
          assertion.
        - the hacking check that nova uses to guard against this has been
          copied to cinder's local hacking checks.

    Fixing the assert_called_once issues also highlighted other mistakes
    in certain tests which were addressed to make the tests pass.

    Due to the nature of mock, this issue is also possible if a method is
    misspelt or just mistakenly used and so the hacking check is only
    addressing one very specific case. That said, it does appear to be a
    common mistake and so is worth singling out.

    Change-Id: Iedcc3f48d91f7ebd8878ccc3bca3d023503774bd
    Closes-Bug: #1394544

Changed in cinder:
status: In Progress → Fix Committed
Mike Perez (thingee)
Changed in cinder:
milestone: none → kilo-1
Thierry Carrez (ttx)
Changed in cinder:
status: Fix Committed → Fix Released
Thierry Carrez (ttx)
Changed in cinder:
milestone: kilo-1 → 2015.1.0
Eric Harney (eharney)
tags: added: tests
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.