Skip to content
This repository has been archived by the owner on Mar 27, 2022. It is now read-only.

Commit

Permalink
added with_children and with_methods for mock() object
Browse files Browse the repository at this point in the history
  • Loading branch information
timbertson committed Mar 2, 2011
1 parent 7d04e85 commit a2b62b4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
43 changes: 35 additions & 8 deletions mocktest/mocking.py
Expand Up @@ -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):
"""
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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):
Expand Down
15 changes: 14 additions & 1 deletion test/mocking_test.py
Expand Up @@ -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
Expand Down

0 comments on commit a2b62b4

Please sign in to comment.