简体   繁体   English

在python 3.4中模拟内部对象的实例方法

[英]mocking an instance method of inner object in python 3.4

I have a class in my code that makes use of a third party library. 我的代码中有一个使用第三方库的类。 I would like to mock the instance method of the object that I obtain by calling an instance method of the library class. 我想通过调用库类的实例方法来模拟获得的对象的实例方法。 I am not clear on how to mock the instance method of this inner object. 我不清楚如何模拟此内部对象的实例方法。 Following is my code: 以下是我的代码:

My class: 我的课:

from a import A

class MyClass(object):
    def __init__(self):
        self.obj = A()

    def do_something(self):
        b = self.obj.get_inner_object()
        result = b.do_inner()
        return result

Here is my test class: 这是我的测试课:

from unittest import TestCase
from unittest.mock import MagicMock
from unittest.mock import patch 
from a import A
class TestMyClass(TestCase):
    def __init__(self):
        self.my_class = MyClass()

    @patch.object(A,'get_inner_object')
    def test_do_something(self, mock_get_inner_object):
        test_res = self.my_class.do_something()

As you can see above, I would like to mock away 2 methods of my library - get_inner_object() and do_inner() which is the instance method of the object returned by get_inner_object(). 正如您在上面看到的,我想模拟掉我的库的2个方法-get_inner_object()和do_inner(),它们是get_inner_object()返回的对象的实例方法。 I was able to mock get_inner_object(), but I am not clear on how to mock the do_inner() method. 我能够模拟get_inner_object(),但不清楚如何模拟do_inner()方法。 Please clarify. 请澄清。 Here's the help I am following: https://www.toptal.com/python/an-introduction-to-mocking-in-python 这是我正在关注的帮助: https : //www.toptal.com/python/an-introduction-to-mocking-in-python

Just mock out all of A : 只是模拟出所有A

@patch('a.A')
def test_do_something(self, mock_A):
    mock_b = mock_A.return_value.get_inner_object.return_value
    mock_b.do_inner.return_value = 'mocked return value'

    test_res = self.my_class.do_something()

    self.assertEqual(test_res, 'mocked return value')

After all, you are testing MyClass here, not A . 毕竟,您在这里测试MyClass ,而不是A

Either way, wether you use @patch.object(A, 'get_inner_object') or patch all of A , the self.obj.get_inner_object() expression calls a Mock instance, so the .return_value attribute is returned at that point. 无论哪种方式,无论您使用@patch.object(A, 'get_inner_object')还是修补所有Aself.obj.get_inner_object()表达式都将调用 Mock实例,因此.return_value将返回.return_value属性。 The do_inner method is just another chained call on that returned mock here, so you can set what is returned for that method by setting the .return_value attribute to something you test for. do_inner方法只是此处返回的模拟的另一个链式调用,因此您可以通过将.return_value属性设置为要测试的内容来设置该方法返回的内容。

To translate that back to your @patch.object() situation, mock_b is then the mock_inner_object.return_value object: 要将其转换回您的@patch.object()情况, mock_b然后是mock_inner_object.return_value对象:

@patch.object(A,'get_inner_object')
def test_do_something(self, mock_get_inner_object):
    mock_b = mock_get_inner_object.return_value
    mock_b.do_inner.return_value = 'mocked return value'

    test_res = self.my_class.do_something()

    self.assertEqual(test_res, 'mocked return value')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM