在Python中模拟assert_Called_with [英] Mocking assert_called_with in Python
问题描述
我在理解以下代码为什么不通过时遇到一些麻烦:
I'm having some trouble understanding why the following code does not pass:
test.py
test.py
import mock
import unittest
from foo import Foo
class TestFoo(unittest.TestCase):
@mock.patch('foo.Bar')
def test_foo_add(self, Bar):
foo = Foo()
foo.add(2, 2)
Bar.add.assert_called_with(2, 2)
if __name__ == '__main__':
unittest.main()
foo.py
foo.py
from bar import Bar
class Foo(object):
def add(self, x, y):
bar = Bar()
return bar.add(x, y)
bar.py
bar.py
class Bar(object):
def add(self, x, y):
print('b.Bar --> Adding {} + {}'.format(x, y))
return x + y
在代码中,Foo.add
创建Bar
的实例,并在调用时返回Bar.add
的结果.为什么在assert_called_with
中测试Bar.add
失败?我相信我在正确的地方嘲笑了Bar
(我嘲笑了foo.Bar
,因为那是正在查找的名称空间,而不是bar.Bar
.)
In the code, Foo.add
creates an instance of Bar
and returns the result of Bar.add
when invoked. Why does testing assert_called_with
for Bar.add
fail? I believe I am mocking the Bar
in the correct place (I am mocking foo.Bar
because that is the namespace which it is being looked up, rather than bar.Bar
).
回溯(最近通话最近): 文件"/Users/iain/PycharmProjects/testing/venv/lib/python2.7/site-packages/mock.py",行1201,已打补丁 return func(* args,** keywargs) 在test_a_b的第12行,文件"test.py" fake_Bar.add.assert_drawn_with(2,2) 在assert_call_with中的文件"/Users/iain/PycharmProjects/testing/venv/lib/python2.7/site-packages/mock.py"的第831行 引发AssertionError('预期的呼叫:%s \ n未呼叫'%(预期的)) AssertionError:预期的调用:add(2,2) 不被叫
Traceback (most recent call last): File "/Users/iain/PycharmProjects/testing/venv/lib/python2.7/site-packages/mock.py", line 1201, in patched return func(*args, **keywargs) File "test.py", line 12, in test_a_b fake_Bar.add.assert_called_with(2, 2) File "/Users/iain/PycharmProjects/testing/venv/lib/python2.7/site-packages/mock.py", line 831, in assert_called_with raise AssertionError('Expected call: %s\nNot called' % (expected,)) AssertionError: Expected call: add(2, 2) Not called
推荐答案
您正在正确的位置模拟方法调用.但是,由于您是从实例中调用方法,因此它是一个绑定方法,因此除其他所有参数外,还将实例作为第一个参数(self
参数)接收.
You are mocking the method call in the right place. However, since you are calling the method from an instance, it is a bound method, and thus receives the instance as the first argument (the self
parameter), in addition to all the other arguments.
编辑:由于Bar
被替换为Mock
实例,因此Bar().add
不知道它是一种方法(因此未绑定任何东西).换句话说,Bar
是Mock
,Bar()
是Mock
,并且Bar().add
也是Mock
.因此,bar.add
是一个新创建的模拟,使用参数(2, 2)
进行调用.断言此调用的一种方法是:
Edit: Since Bar
is replaced with a Mock
instance, Bar().add
does not know it's a method (and hence is not bound to anything). In other words, Bar
is a Mock
, Bar()
is a Mock
, and Bar().add
is also a Mock
. bar.add
is thus a newly created mock, called with arguments (2, 2)
. One way of asserting this call would be:
@mock.patch('foo.Bar')
def test_foo_add(self, Bar):
foo = Foo()
foo.add(2, 2)
Bar.return_value.add.assert_called_with(2, 2)
根据实际代码的外观,您可能需要模拟方法而不是类:
Depending on how your actual code looks, you may want to instead mock the method rather than the class:
@mock.patch('foo.Bar.add')
def test_foo_add(self, bar_add):
foo = Foo()
foo.add(2, 2)
bar_add.assert_called_with(2, 2)
这篇关于在Python中模拟assert_Called_with的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!