Skip to content
Browse files

Merge remote-tracking branch 'poiati/master'

  • Loading branch information...
2 parents fdc2df3 + dca4ba3 commit 6dab151ea59189f4233bc26bc67fba9394bf832e @garybernhardt committed
Showing with 93 additions and 5 deletions.
  1. +13 −1 dingus.py
  2. +80 −0 tests/test_assert_call.py
  3. +0 −3 tests/test_call.py
  4. +0 −1 tests/test_isolation.py
View
14 dingus.py
@@ -242,6 +242,19 @@ def reset(self):
self.calls = CallList()
self._children = {}
+ def assert_call(self, *args, **kwargs):
+ expected_call = self.calls('()', *args, **kwargs)
+ if expected_call:
+ return
+ recorded_calls = self.calls
+ calls_description = "No calls recorded" if not recorded_calls \
+ else "Recorded calls: %s" % recorded_calls
+ message = "Expected a call to: '%s', " % self + \
+ "args: %s, kwargs: %s, " % (args, kwargs) + \
+ "\n" + calls_description
+
+ raise AssertionError(message)
+
def _get_return_value(self):
if self._return_value is NoReturnValue:
self._return_value = self._create_child('()')
@@ -376,4 +389,3 @@ def exception_raiser(exception):
def raise_exception(*args, **kwargs):
raise exception
return raise_exception
-
View
80 tests/test_assert_call.py
@@ -0,0 +1,80 @@
+from dingus import Dingus
+from nose.tools import raises
+
+
+class AssertCallTest(object):
+
+ def setup(self):
+ self.ding = Dingus('ding')
+
+class WhenCallsExists(AssertCallTest):
+
+ def should_not_raise_any_error_simple_call(self):
+ self.ding.foo()
+
+ self.ding.foo.assert_call()
+
+ def should_not_raise_any_error_with_args(self):
+ self.ding.foo('bar')
+
+ self.ding.foo.assert_call()
+ self.ding.foo.assert_call('bar')
+
+ def should_not_raise_any_error_with_args_and_kwargs(self):
+ self.ding.foo('bar', qux=1)
+
+ self.ding.foo.assert_call()
+ self.ding.foo.assert_call('bar')
+ self.ding.foo.assert_call('bar', qux=1)
+
+class WhenThereIsNoCallsForTheMatchedArgs(AssertCallTest):
+
+ @raises(AssertionError)
+ def should_raise_an_assertion_error(self):
+ self.ding.foo.assert_call()
+
+ @raises(AssertionError)
+ def should_raise_an_assertion_error_other_method_call(self):
+ self.ding.bar()
+
+ self.ding.foo.assert_call()
+
+ @raises(AssertionError)
+ def should_raise_an_assertion_error_with_args(self):
+ self.ding.foo()
+
+ self.ding.foo.assert_call('bar')
+
+ @raises(AssertionError)
+ def should_raise_an_assertion_error_with_args_and_kargs(self):
+ self.ding.foo('bar')
+
+ self.ding.foo.assert_call('bar', qux=1)
+
+ def should_show_a_friendly_error_message(self):
+ self._test_expectation_message('foo')
+
+ def should_show_a_friendly_error_message_with_args(self):
+ self._test_expectation_message('foo', 'baz', 'qux')
+
+ def should_show_a_friendly_error_message_with_args_and_kargs(self):
+ self._test_expectation_message('foo', 'baz', 'qux', one=1, two=2)
+
+ def _test_expectation_message(self, method, *args, **kwargs):
+ try:
+ dingus = getattr(self.ding, method)
+ dingus.assert_call(*args, **kwargs)
+ except AssertionError, e:
+ self._assert_message(e.message, dingus, args, kwargs)
+ else:
+ assert False, 'should not be here'
+
+ def _assert_message(self, message, dingus, args, kwargs):
+ expected, recorded_calls = message.split('\n')
+
+ assert "Expected a call to: '%s', args: %s, kwargs: %s, " % (dingus, args, kwargs)
+
+ if not self.ding.calls:
+ assert "No calls recorded" == recorded_calls
+ else:
+ assert ("Recorded calls: %s" % self.ding.calls) == recorded_calls
View
3 tests/test_call.py
@@ -29,6 +29,3 @@ def setup(self):
call_str = pickle.dumps(self.call, pickle.HIGHEST_PROTOCOL)
self.call = pickle.loads(call_str)
-
-
-
View
1 tests/test_isolation.py
@@ -31,7 +31,6 @@ def should_name_dingus_after_patched_object(self):
with patch('urllib2.urlopen'):
assert str(urllib2.urlopen) == '<Dingus urllib2.urlopen>'
-
class WhenIsolating:
def should_isolate(self):
@isolate("os.popen")

0 comments on commit 6dab151

Please sign in to comment.
Something went wrong with that request. Please try again.