[tor-dev] stem mock_method usage

Damian Johnson atagar at torproject.org
Sat Dec 8 02:22:39 UTC 2012


Hi Sean. You're using the mock_method() function correctly...

========================================

class Foo:
  def greeting(self):
    return 'hi'

f = Foo()
print f.greeting()

mocking.mock_method(Foo, 'greeting', mocking.return_value('bye'))

print f.greeting()

========================================

atagar at morrigan:~/Desktop/stem$ python example.py
hi
bye

========================================

The trouble is that the Controller __dict__ is a dictproxy (a
read-only dictionary). After some experimentation this seems to be a
product of extending object...

========================================

>>> class Foo:
...   pass

>>> type(Foo.__dict__)
<type 'dict'>

>>> class Bar(object):
...   pass

>>> type(Bar.__dict__)
<type 'dictproxy'>

========================================

So... um, shame on us for doing the right thing. ;)

We've probably never encountered this before because the mock_method()
function is presently completely unused...

atagar at morrigan:~/Desktop/stem$ grep -Rl "mock_method" * | grep -v '.pyc'
test/mocking.py

It was added in a500dbc to help test the BaseController, then its
usage was removed in 4ff7efe. Strangely method mocking hasn't come up
again since then. Unfortunately I don't have a good suggestion on how
to work around this...


More information about the tor-dev mailing list