Skip to content

Commit

Permalink
Better error for trying to mock builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
has207 committed Jan 29, 2011
1 parent 4376fd3 commit c9182af
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
9 changes: 9 additions & 0 deletions flexmock.py
Expand Up @@ -32,6 +32,13 @@ class FlexmockException(Exception):
pass


class AttemptingToMockBuiltin(Exception):
def __str__(self):
out = 'Python does not allow you to mock builtin modules. '
out += 'Consider wrapping it in a class you can mock instead'
return out


class InvalidMethodSignature(FlexmockException):
pass

Expand Down Expand Up @@ -636,6 +643,8 @@ def _generate_mock(flexmock_class, object_or_class=None, **kwargs):
mock = flexmock_class()
try:
mock._setup_mock(object_or_class, **kwargs)
except TypeError:
raise AttemptingToMockBuiltin
except AlreadyMocked:
mock = object_or_class
return mock
Expand Down
10 changes: 9 additions & 1 deletion flexmock_test.py
Expand Up @@ -2,6 +2,7 @@
from flexmock import FlexMock
from flexmock import AlreadyMocked
from flexmock import AndExecuteNotSupportedForClassMocks
from flexmock import AttemptingToMockBuiltin
from flexmock import Expectation
from flexmock import FlexmockContainer
from flexmock import FlexmockException
Expand Down Expand Up @@ -804,7 +805,6 @@ class User: pass
mock = flexmock(user)
try:
flexmock(user).should_receive('nonexistent')
assert False
except MethodDoesNotExist:
assert True
return
Expand All @@ -820,6 +820,14 @@ def test_flexmock_should_not_explode_on_unicode_formatting(self):
'method', {'kargs' : (unichr(0x86C7),), 'kwargs' : {}})
assert formatted == 'method("%s")' % unichr(0x86C7)

def test_flexmock_should_give_reasonable_error_for_builtins(self):
try:
flexmock(object).should_receive('time')
except AttemptingToMockBuiltin:
assert True
return
assert False


if __name__ == '__main__':
unittest.main()
2 changes: 1 addition & 1 deletion run_tests.sh
Expand Up @@ -12,6 +12,6 @@ done
if test -f "$(which nosetests)"; then
echo nosetests
nosetests flexmock_test.py
echo
else
echo nosetests NOT FOUND
fi

0 comments on commit c9182af

Please sign in to comment.