【问题标题】:Error when trying to use `assert not mock.method.called`尝试使用 `assert not mock.method.called` 时出错
【发布时间】:2014-11-19 16:41:59
【问题描述】:

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

AttributeError: MockCallable instance has no attribute 'called'

我正在运行 Python 2.7.1 和 Python Mock 0.1.0 进行测试。谷歌说: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'

【问题讨论】:

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


    【解决方案1】:

    您必须通过pip 更新您的mock 库。 called 属性是在 0.4.0 中引入的,正如您在 http://www.voidspace.org.uk/python/mock/changelog.html#version-0-4-0 中看到的那样

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-27
      • 1970-01-01
      • 2018-10-27
      • 1970-01-01
      • 2017-10-19
      • 2021-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多