仅模拟对象上的单个方法

Mocking only a single method on an object(仅模拟对象上的单个方法)
本文介绍了仅模拟对象上的单个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我熟悉其他语言中的其他模拟库,例如 Java 中的 Mockito,但 Python 的 mock 库让我感到困惑.

我想测试以下课程.

类 MyClassUnderTest(object):def 子方法(自我,*args):do_dangerous_things()def main_method(self):self.submethod("什么都没有.")

在我的测试中,我想确保在执行 main_method 时调用了 submethod 并且使用正确的参数调用它.我不希望 submethod 运行,因为它会做危险的事情.

我完全不确定如何开始使用它.Mock 的文档非常难以理解,我不确定要模拟什么或如何模拟它.

我怎样才能模拟 submethod 函数,同时保留 main_method 中的功能?

解决方案

我想你要找的是 mock.patch.object

用 mock.patch.object(MyClassUnderTest, "submethod") 作为 submethod_mocked:submethod_mocked.return_value = 13MyClassUnderTest().main_method()submethod_mocked.assert_call_once_with(user_id, 100, self.context,self.account_type)

这里是小描述

 patch.object(目标, 属性, new=DEFAULT,规格=无,创建=假,规格集=无,autospec=None, new_callable=None, **kwargs)

<块引用>

使用模拟对象修补对象(目标)上的命名成员(属性).

I'm familiar with other mocking libraries in other languages such as Mockito in Java, but Python's mock library confuses the life out of me.

I have the following class which I would like to test.

class MyClassUnderTest(object):

    def submethod(self, *args):
       do_dangerous_things()

    def main_method(self):
       self.submethod("Nothing.")

In my tests, I'd like to make sure that the submethod was called when main_method was executed and that it was called with the right arguments. I don't want submethod to run, as it does dangerous things.

I'm entirely unsure as to how to get started with this. Mock's documentation is incredibly hard to understand and I'm not sure what to even mock or how to mock it.

How can I mock the submethod function, while leaving the functionality in main_method alone?

解决方案

I think what you are looking for is mock.patch.object

with mock.patch.object(MyClassUnderTest, "submethod") as submethod_mocked:
    submethod_mocked.return_value = 13
    MyClassUnderTest().main_method()
    submethod_mocked.assert_called_once_with(user_id, 100, self.context,
                                             self.account_type)

Here is small description

 patch.object(target, attribute, new=DEFAULT, 
              spec=None, create=False, spec_set=None, 
              autospec=None, new_callable=None, **kwargs)

patch the named member (attribute) on an object (target) with a mock object.

这篇关于仅模拟对象上的单个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

patching a class yields quot;AttributeError: Mock object has no attributequot; when accessing instance attributes(修补类会产生“AttributeError:Mock object has no attribute;访问实例属性时)
How to mock lt;ModelClassgt;.query.filter_by() in Flask-SqlAlchemy(如何在 Flask-SqlAlchemy 中模拟 lt;ModelClassgt;.query.filter_by())
FTPLIB error socket.gaierror: [Errno 8] nodename nor servname provided, or not known(FTPLIB 错误 socket.gaierror: [Errno 8] nodename nor servname provided, or not known)
Weird numpy.sum behavior when adding zeros(添加零时奇怪的 numpy.sum 行为)
Why does the #39;int#39; object is not callable error occur when using the sum() function?(为什么在使用 sum() 函数时会出现 int object is not callable 错误?)
How to sum in pandas by unique index in several columns?(如何通过几列中的唯一索引对 pandas 求和?)