bool(mock) fails
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
Mocker |
Fix Released
|
Undecided
|
Unassigned |
Bug Description
The following code fails with "TypeError: __nonzero__ should return bool or int, returned Mock"
import mocker as mk
mocker = mk.Mocker()
thing = mocker.mock()
bool(thing)
mocker.result(True)
mocker.replay()
bool(thing)
mocker.restore()
mocker.verify()
The problem is that Mock.__nonzero__ is returning a mock. The version below fixes that, by
returning True, but then you can't use expect because it needs the mock.
def __nonzero__(self):
try:
result = self.__
if type(result) is Mock:
return result
except MatchError, e:
return True
Issues 196388 and 179072 seem closely related to this.
Changed in mocker: | |
status: | Fix Committed → Fix Released |
I'm also seeing this, as a result of using the 'in' operator on a mock in Python 2.6.
Failed example: python2. 6/doctest. py", line 1231, in __run
compileflags, 1) in test.globs txt[8]> ", line 1, in <module>
'hi' in container # doctest: +ELLIPSIS
Exception raised:
Traceback (most recent call last):
File "/usr/lib64/
File "<doctest mock_container.
'hi' in container # doctest: +ELLIPSIS
TypeError: __nonzero__ should return bool or int, returned Mock