diff options
-rw-r--r-- | mocker.py | 32 | ||||
-rwxr-xr-x | test.py | 16 |
2 files changed, 32 insertions, 16 deletions
@@ -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)) @@ -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() |