【问题标题】:Asserting an object passed into a mocked method in the python mock在 python 模拟中断言传递给模拟方法的对象
【发布时间】:2013-07-12 16:13:15
【问题描述】:

假设我有一个名为 Client 的类,它创建 Request 类的对象并将其传递给 Connection 对象的方法:

class Client(object):
    def __init__(self, connection):
        self._conn = connection

    def sendText(plaintext):
        self._conn.send(Request(0, plaintext))

我想断言传递给 Connection.send 方法的对象以检查其属性。我从创建一个模拟的 Connection 类开始:

conn = Mock()

client = Client(conn)
client.sendText('some message')

然后我想要类似的东西:

conn.send.assert_called_with(
    (Request,
    {'type': 0, 'text': 'some message'})
)

其中 'type' 和 'text' 是 Request 的属性。有没有办法在 python 的模拟中做到这一点?我在文档中找到的只是简单的数据示例。 我可以用 mock.patch 装饰器通过用断言对象字段的方法替换原始的“发送”方法来完成它:

def patchedSend(self, req):
    assert req.Type == 0

with mock.patch.object(Connection, 'send', TestClient.patchedSend):
    ...

但在这种情况下,我必须为每个方法检查定义一个单独的模拟函数,并且我无法检查(无需额外编码)该函数是否已被调用。

【问题讨论】:

    标签: python mocking


    【解决方案1】:

    你可以得到模拟的最后一个参数

    request, = conn.send.call_args
    

    然后断言关于它的属性。如果您希望工具能够表达关于事物的更复杂的断言,您可以安装PyHamcrest

    注意:不要在单元测试中使用assert。使用assertEqualassertTrue 等断言方法。断言方法不会被意外关闭,它们可以提供比assert 语句更有用的消息。

    【讨论】:

    • 这就是我所需要的。谢谢。
    【解决方案2】:

    嗯,我认为在这种特定情况下,最简单和更好的方法是创建一个函数来创建请求,然后模拟它。

    例如,它会是这样的:

    class Client(object):
        def __init__(self, connection):
            self._conn = connection
    
        def _create_request(self, plain_text):
            return Request(0, plain_text)
    
        def send_text(self, plaintext):
            self._conn.send(self._create_request(plain_text))
    

    然后,在测试中,您可以模拟 _create_request 以返回一些特定的值,然后断言 send_text 是用它调用的。

    您也可以按照建议通过 call_args 获取参数,但我认为这样看起来更好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-15
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 2011-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多