Closed GoogleCodeExporter closed 9 years ago
I've found this annoying when passing mocks into frameworks that check if an
object is callable and, if so, call it. (Examples I know of are both template
languages: Django templates and ZPT.) As a workaround I end up adding stuff
like this to my tests:
mock_foo.return_value = mock_foo
... which isn't hard, but I don't like that it's not self-explanatory to
somebody reading my tests - too much black magic smell.
Original comment by sli...@gmail.com
on 14 Jun 2011 at 6:38
The fix I was *going* to put in place was to have calling a mock that shouldn't
be callable raise a TypeError exception. That wouldn't solve your use-case. The
best way to solve that would be to have a new class NonCallableMock that
doesn't have a __call__ method at all. That introduces some new complexity
(attributes of non-callable mocks would still need to be callable) but would
solve both use cases.
Original comment by fuzzyman
on 15 Jun 2011 at 9:41
This also applies to autospec.
When patching a class it should be callable. The return value should have the
same spec (inherit) but should only be callable if the class has a __call__
method.
If patching an instance (or builtin type) then the mock should only be callable
if the original is callable (has a __call__ method).
Original comment by fuzzyman
on 17 Jun 2011 at 4:50
Original comment by fuzzyman
on 18 Jun 2011 at 12:03
Committed in repo, will be in 0.8 alpha 2. The fix is the addition of two new
mock classes NonCallableMock and NonCallableMagicMock.
Original comment by fuzzyman
on 3 Jul 2011 at 11:18
Original issue reported on code.google.com by
fuzzyman
on 7 Mar 2011 at 11:40