为什么两个魔术模拟实例在ID中有所不同?

发布于 2025-01-22 14:29:42 字数 1777 浏览 2 评论 0原文

阅读本教程后,我正在尝试使用Patch()将模拟注入呼叫: https://kimsereylam.com/python/2021/03/03/how-to-to-poth-to-path-to-patch-patch-in-patch-in-python-unittest.html一下期望模拟ID与实际不同。

这是要说明的代码段:

my_class.py

class MyClass:
    def __init__(self):
        self.value = 5

run.py

from my_class import MyClass

def call_first():
    o = MyClass()
    print(o)
    call_second(o)
    
def call_second(myclass):
    print("second is called")
    print(myclass)

main。 PY

from unittest.mock import patch
from my_class import MyClass
from run import call_first, call_second

with patch("run.MyClass") as myclass_mock:
    print(myclass_mock)
    print(call_first())
with patch("run.call_second") as call_second_mock:
    call_second_mock.assert_called_once_with(myclass_mock)
        

输出是:

<MagicMock name='MyClass' id='140711608367712'>
<MagicMock name='MyClass()' id='140711598743312'>
second is called
<MagicMock name='MyClass()' id='140711598743312'>
None
Traceback (most recent call last):
  File "main.py", line 9, in <module>
    call_second_mock.assert_called_once_with(myclass_mock)
  File "/usr/lib/python3.8/unittest/mock.py", line 924, in assert_called_once_with
    raise AssertionError(msg)
AssertionError: Expected 'call_second' to be called once. Called 0 times.

我错过了什么?我正在尝试使用patch(),因此我可以模拟call_first()内实例化并传递给call_second()的myClass()对象。

I am trying to use patch() to inject mock into a call after reading this tutorial: https://kimsereylam.com/python/2021/03/19/how-to-use-patch-in-python-unittest.html, but the expect mock ID is different than actual.

Here is snippet of code to illustrate:

my_class.py

class MyClass:
    def __init__(self):
        self.value = 5

run.py

from my_class import MyClass

def call_first():
    o = MyClass()
    print(o)
    call_second(o)
    
def call_second(myclass):
    print("second is called")
    print(myclass)

main.py

from unittest.mock import patch
from my_class import MyClass
from run import call_first, call_second

with patch("run.MyClass") as myclass_mock:
    print(myclass_mock)
    print(call_first())
with patch("run.call_second") as call_second_mock:
    call_second_mock.assert_called_once_with(myclass_mock)
        

The output is:

<MagicMock name='MyClass' id='140711608367712'>
<MagicMock name='MyClass()' id='140711598743312'>
second is called
<MagicMock name='MyClass()' id='140711598743312'>
None
Traceback (most recent call last):
  File "main.py", line 9, in <module>
    call_second_mock.assert_called_once_with(myclass_mock)
  File "/usr/lib/python3.8/unittest/mock.py", line 924, in assert_called_once_with
    raise AssertionError(msg)
AssertionError: Expected 'call_second' to be called once. Called 0 times.

What did I miss? I am trying to use patch() so I can mock the MyClass() object that is instantiated inside call_first() and passed to call_second().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弃爱 2025-01-29 14:29:42
with patch("run.MyClass") as myclass_mock:
    with patch("run.call_second") as call_second_mock:
        #print(myclass_mock)
        instance = myclass_mock.return_value
        print(instance)
        print(call_first())
        call_second_mock.assert_called_once_with(instance)
  1. 我需要使用.return_value来获取类实例的magionmock
  2. 我应该模拟call_second首先
with patch("run.MyClass") as myclass_mock:
    with patch("run.call_second") as call_second_mock:
        #print(myclass_mock)
        instance = myclass_mock.return_value
        print(instance)
        print(call_first())
        call_second_mock.assert_called_once_with(instance)
  1. I need to use .return_value to get the MagicMock for the class instance
  2. I should mock the call_second first
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文