尝试使用`assert not mock.method.called`时出错

时间:2014-11-19 16:42:00

标签: python unit-testing python-2.7 python-unittest python-mock

我试图断言使用Python Mock不调用方法。不幸的是,我似乎无法克服这个错误:

AttributeError: MockCallable instance has no attribute 'called'

我正在为我的测试运行Python 2.7.1和Python Mock 0.1.0。 Google说:No results found for "AttributeError: MockCallable instance has no attribute 'called'".如何解决此错误?

以下是测试:

import unittest2
import main
from mock import Mock

class TestCli(unittest2.TestCase):
    def setUp(self):
        self.mockView = Mock()
        self.mockFriendManager = Mock()
        self.mockedCli = main.CLI(self.mockView, self.mockFriendManager)

[...]

    def testCliDoesntGetFriendPropertiesWhenNotSelected(self):
        view = Mock( { "requestResponse":2 } )
        friendManager = Mock()
        cli = main.CLI(view, friendManager)
        cli.outputMenu()
        assert not friendManager.getFriendProperties.called, 'hello'

1 个答案:

答案 0 :(得分:1)

您必须按mock更新pip库。正如您在http://www.voidspace.org.uk/python/mock/changelog.html#version-0-4-0

中所看到的,在{0.4}中引入了属性called

无论如何通过更新它你将获得许多更有用的设施和工具。