
Damian, I am attempting to write a test using test.mocking.mock_method, but I do not understand how to use it correctly. Could you give me pointers on the following smallest (non-)working test case. import stem from stem.control import Controller import test.mocking as mocking socket = stem.socket.ControlSocket() controller = Controller(socket) mocking.mock_method(Controller, 'get_version', mocking.return_value(stem.version.Version('0.1.0.14'))) controller.get_version() The above code fails with: Traceback (most recent call last): File "mock_method-test.py", line 8, in <module> mocking.mock_method(Controller, 'get_version', mocking.return_value(stem.version.Version('0.1.0.14'))) File "stem.dev/test/mocking.py", line 317, in mock_method target_class.__dict__[method_name] = mock_wrapper TypeError: 'dictproxy' object does not support item assignment -- Sean Robinson

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@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@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...
participants (2)
-
Damian Johnson
-
Sean Robinson