Skip to content

Instantly share code, notes, and snippets.

@lukearno
Created May 12, 2012 22:08
Show Gist options
  • Save lukearno/2669371 to your computer and use it in GitHub Desktop.
Save lukearno/2669371 to your computer and use it in GitHub Desktop.
An example that I think might show an opportunity to enhance Python Flexmock.
from flexmock import flexmock
# foo() is the code under test
# other.bar() is a dependency we are mocking
def foo(other):
other.bar(1, 2)
class OldVersionOfDep():
def bar(self, x, y):
print x, y
def test_foo_with_old():
other = OldVersionOfDep()
(flexmock(other)
.should_receive('bar')
.with_args(1, 2)
.replace_with(lambda x, y: None))
foo(other)
class NewVersionOfDep():
def bar(self, x, y, z):
print x, y, z
def test_foo_with_new():
other = NewVersionOfDep()
(flexmock(other)
.should_receive('bar')
.with_args(1, 2)
.replace_with(lambda x, y: None))
foo(other)
print "Old version of dependency."
test_foo_with_old() # Passes.
foo(OldVersionOfDep()) # Works.
# -> 1 2
print "New version of dependency."
test_foo_with_new() # Passes, but I wish it didn't...
foo(NewVersionOfDep()) # Broken! (Flexmock could catch this!)
# -> TypeError: bar() takes exactly 4 arguments (3 given)
@lukearno
Copy link
Author

The point here is that when the things you mock change there signatures, it would be nice to get an error. I think that's a common class of bug and it would be reasonable cheap to detect.

@lukearno
Copy link
Author

Feature implemented! Go Python Flexmock!

has207/flexmock#79 (comment)

yellowbus:fmock luke$ python test.py
Old version of dependency.
1 2
New version of dependency.
Traceback (most recent call last):
  File "test.py", line 42, in <module>
    test_foo_with_new()  # Passes, but I wish it didn't...
  File "test.py", line 32, in test_foo_with_new
    .with_args(1, 2)
  File "/Users/luke/src/fmock/src/flexmock/flexmock.py", line 349, in with_args
    self._verify_signature_match(*kargs, **kwargs)
  File "/Users/luke/src/fmock/src/flexmock/flexmock.py", line 250, in _verify_signature_match
    (self.name, minimum, total_positional))
flexmock.FlexmockError: bar requires at least 3 arguments, expectation provided 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment