Question is as in the heading, how can I mock select.select by example to test my thread run function. the test function fails with
ready = select.select([self.sock], [], [], 5)
TypeError: fileno() returned a non-integer
and the type print gives
type 'builtin_function_or_method'
so clearly select.select is NOT mocked in the thread's scope while in the test it is...(assert isinstance)
import select
import threading
RECEIVE_BYTES = 256
class Bar(threading.Thread):
def __init__(self, sock):
threading.Thread.__init__(self)
self.sock = sock
def run(self):
print type(select.select)
ready = select.select([self.sock],[],[],5)
if ready[0]:
print self.sock.recv(RECEIVE_BYTES)
the test is as follows in another module
def test_run(self):
with patch("select.select"):
select.select.return_value = [True]
mock_sock = MagicMock()
foo = Bar(mock_sock)
assert isinstance(select.select, MagicMock)
foo.start()
tests are run via nose