Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking functions with FlexMock in Python?

Tags:

python

mocking

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

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

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

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

like image 597
ustun Avatar asked Jul 12 '11 12:07

ustun


1 Answers

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')

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

like image 51
Herman Sheremetyev Avatar answered Sep 28 '22 03:09

Herman Sheremetyev