在Python中模拟assert_Called_with [英] Mocking assert_called_with in Python

查看:309
本文介绍了在Python中模拟assert_Called_with的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解以下代码为什么不通过时遇到一些麻烦:

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不知道它是一种方法(因此未绑定任何东西).换句话说,BarMockBar()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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆