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

问题描述

我知道如何使用flexmock在Python中模拟方法,例如

I know how to mock methods in Python using flexmock, like

 flexmock(subprocess).should_receive('call').replace_with(my_func)

一个模拟函数如何在对象之外(例如通过from glob import glob而不是import glob导入的glob)起作用?

How does one mock functions outside objects, or for example glob, which was imported via from glob import glob instead of import glob?

我发现使用python模拟的模拟功能是一个类似的问题,但是没有回答我的问题.

I have found mocking functions using python mock as a similar question, but it doesn't answer my question.

推荐答案

由于要将glob()函数直接导入本地名称空间,因此必须获取当前模块的句柄.

Since you're importing the glob() function directly into the local namespace you have to get a handle on the current module.

from flexmock import flexmock
from glob import glob
import sys

flexmock(sys.modules[__name__]).should_receive('glob')

您还可以执行将glob作为glob_module导入"或类似的操作,以避免sys.modules查找.

You could also do an "import glob as glob_module" or something along those lines to avoid the sys.modules lookup.

这篇关于在Python中使用FlexMock模拟功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 09:11