Permalink
Browse files

added with_children and with_methods for mock() object

  • Loading branch information...
1 parent 7d04e85 commit a2b62b4a9f8fefa85d0c3d88851d8246cf0fc6de @timbertson committed Mar 2, 2011
Showing with 49 additions and 9 deletions.
  1. +35 −8 mocktest/mocking.py
  2. +14 −1 test/mocking_test.py
View
@@ -88,6 +88,19 @@ def mock_expect(obj, name):
def _special_method(name):
return name.startswith('__') and name.endswith('__')
+
+def assign_kwargs_children(self, **children):
+ [setattr(self, k, v) for k, v in children.items()]
+ return self
+
+def assign_kwargs_methods(self, **methods):
+ def do_return(return_value):
+ return lambda *a, **k: return_value
+
+ for k,v in methods.items():
+ setattr(self, k, do_return(v))
+ return self
+
from lib.realsetter import RealSetter
class RecursiveAssignmentWrapper(RealSetter):
"""
@@ -110,21 +123,15 @@ def children(self, **children):
>>> obj.y
'child y'
"""
- [setattr(self, k, v) for k, v in children.items()]
- return self
+ return assign_kwargs_children(self, **children)
def methods(self, **methods):
"""
Set child methods via kwargs, e.g.:
>>> modify(obj).children(x=1, y=mock('child y'))
"""
- def do_return(return_value):
- return lambda *a, **k: return_value
-
- for k,v in methods.items():
- setattr(self, k, do_return(v))
- return self
+ return assign_kwargs_methods(self, **methods)
def copying(self, other, value=lambda *a, **kw: None):
"""
@@ -195,6 +202,26 @@ def __call__(self, *a, **kw):
self.received_calls.append(Call(a,kw, stack=True))
return None
+ def with_children(self, **children):
+ """
+ Set children via kwargs, e.g.:
+
+ >>> mock("name").with_children(x=1, y='child y')
+ >>> obj.x
+ 1
+ >>> obj.y
+ 'child y'
+ """
+ return assign_kwargs_children(self, **children)
+
+ def with_methods(self, **methods):
+ """
+ Set child methods via kwargs, e.g.:
+
+ >>> mock("name").with_children(x=1, y=mock('child y'))
+ """
+ return assign_kwargs_methods(self, **methods)
+
def stub_method(obj, name):
assert MockTransaction.started, "Mock transaction has not been started. Make sure you are inheriting from mocktest.TestCase"
if _special_method(name) and not isinstance(obj, type):
View
@@ -275,14 +275,27 @@ def test_multiple_any_instance_after_normal_args(self):
class TestMockCreation(TestCase):
@passing
def test_creation_methods_kwargs(self):
+ obj = mock('foo').with_methods(x=1, y=2)
+ assert obj.x() == 1
+ assert obj.x(1,2,3) == 1
+ assert obj.y() == 2, obj.y()
+
+ @passing
+ def test_creation_children_kwargs(self):
+ obj = mock('foo').with_children(x=1, y=2)
+ assert obj.x == 1
+ assert obj.y == 2
+
+ @passing
+ def test_modification_methods_kwargs(self):
obj = mock('foo')
modify(obj).methods(x=1, y=2)
assert obj.x() == 1
assert obj.x(1,2,3) == 1
assert obj.y() == 2, obj.y()
@passing
- def test_creation_children_kwargs(self):
+ def test_modification_children_kwargs(self):
obj = mock('foo')
modify(obj).children(x=1, y=2)
assert obj.x == 1

0 comments on commit a2b62b4

Please sign in to comment.