summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--NEWS4
-rw-r--r--mocker.py11
-rwxr-xr-xtest.py3
3 files changed, 16 insertions, 2 deletions
diff --git a/NEWS b/NEWS
index 5066402..25a9885 100644
--- a/NEWS
+++ b/NEWS
@@ -9,6 +9,10 @@
isn't called, and that may not be obvious, so a hook won't be
exposed for now.
+- When using an unexistent import path for Mocker.proxy(), raise an
+ ImportError on the base module, rather than using the actual
+ string as the object (#162315).
+
0.8 (2007-11-11)
================
diff --git a/mocker.py b/mocker.py
index cb34664..71d3ac7 100644
--- a/mocker.py
+++ b/mocker.py
@@ -428,7 +428,10 @@ class MockerBase(object):
is True), or explicitly (using the L{passthrough()} method
on the event).
- @param object: Real object to be proxied.
+ @param object: Real object to be proxied, and replaced by the mock
+ on replay mode. It may also be an "import path",
+ such as C{"time.time"}, in which case the object
+ will be the C{time} function from the C{time} module.
@param spec: Method calls will be checked for correctness against
the given object, which may be a class or an instance
where attributes will be looked up. Defaults to the
@@ -459,6 +462,8 @@ class MockerBase(object):
object = __import__(module_path, {}, {}, [""])
except ImportError:
attr_stack.insert(0, import_stack.pop())
+ if not import_stack:
+ raise
continue
else:
for attr in attr_stack:
@@ -481,7 +486,9 @@ class MockerBase(object):
namespaces, class namespaces, instance namespaces, and so on.
@param object: Real object to be proxied, and replaced by the mock
- on replay mode.
+ on replay mode. It may also be an "import path",
+ such as C{"time.time"}, in which case the object
+ will be the C{time} function from the C{time} module.
@param spec: Method calls will be checked for correctness against
the given object, which may be a class or an instance
where attributes will be looked up. Defaults to the
diff --git a/test.py b/test.py
index 236aa0c..ecbb74d 100755
--- a/test.py
+++ b/test.py
@@ -875,6 +875,9 @@ class MockerTest(unittest.TestCase):
module = self.mocker.proxy("os.path", name="mock")
self.assertEquals(module.__mocker_name__, "mock")
+ def test_proxy_with_unexistent_module(self):
+ self.assertRaises(ImportError, self.mocker.proxy, "unexistent.module")
+
def test_replace(self):
from os import path
obj = object()