summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mocker.py32
-rwxr-xr-xtest.py16
2 files changed, 32 insertions, 16 deletions
diff --git a/mocker.py b/mocker.py
index f31cf53..d4f15a9 100644
--- a/mocker.py
+++ b/mocker.py
@@ -385,6 +385,22 @@ class MockerBase(object):
explicitly requested via the L{passthrough()}
method.
"""
+ if isinstance(object, basestring):
+ if name is None:
+ name = object
+ import_stack = object.split(".")
+ attr_stack = []
+ while import_stack:
+ module_path = ".".join(import_stack)
+ try:
+ object = __import__(module_path, {}, {}, [""])
+ except ImportError:
+ attr_stack.insert(0, import_stack.pop())
+ continue
+ else:
+ for attr in attr_stack:
+ object = getattr(object, attr)
+ break
if spec is True:
spec = object
if type is True:
@@ -422,22 +438,6 @@ class MockerBase(object):
explicitly requested via the L{passthrough()}
method.
"""
- if isinstance(object, basestring):
- if name is None:
- name = object
- import_stack = object.split(".")
- attr_stack = []
- while import_stack:
- module_path = ".".join(import_stack)
- try:
- object = __import__(module_path, {}, {}, [""])
- except ImportError:
- attr_stack.insert(0, import_stack.pop())
- continue
- else:
- for attr in attr_stack:
- object = getattr(object, attr)
- break
mock = self.proxy(object, spec, type, name, passthrough)
event = self._get_replay_restore_event()
event.add_task(ProxyReplacer(mock))
diff --git a/test.py b/test.py
index 2d74217..315c507 100755
--- a/test.py
+++ b/test.py
@@ -775,6 +775,22 @@ class MockerTest(unittest.TestCase):
self.assertEquals(mock.__mocker_spec__, C)
self.assertEquals(mock.__mocker_passthrough__, False)
+ def test_proxy_with_submodule_string(self):
+ from os import path
+ module = self.mocker.proxy("os.path")
+ self.assertEquals(type(module), Mock)
+ self.assertEquals(type(module.__mocker_object__), ModuleType)
+ self.assertEquals(module.__mocker_name__, "os.path")
+ self.assertEquals(module.__mocker_object__, path)
+
+ def test_proxy_with_module_function_string(self):
+ mock = self.mocker.proxy("os.path.join.func_name")
+ self.assertEquals(mock.__mocker_object__, "join")
+
+ def test_proxy_with_string_and_name(self):
+ module = self.mocker.proxy("os.path", name="mock")
+ self.assertEquals(module.__mocker_name__, "mock")
+
def test_replace(self):
from os import path
obj = object()