mock any instance

P

Phlip

Pythonistas:

One important design principle is "construction encapsulation". That's
where nobody creates anything, they always use things passed to them.

Without this principle, when I need to mock the encapsulated item,
some mock libraries provide an "any instance" facility.

For example, here's a mock on one method of only one object:

object = Thang()
object.method = Mock()
object.method() # <-- calls the mock, not the real method

I need to mock the method on any instance spawned by a given class:

Thang.method = MockAnyInstance()
object = Thang()
object.method() # <-- calls the mock, not the real method

That will avoid "cutting a hole in the bulkhead" just to get to the
'object = Thang()' line, to let me spoof out the Thang.

Does anyone have any idea how to do that with the good-old Mocker
here?

http://python-mock.sourceforge.net/

if I should use a different mocker, I would prefer it behave like that
mock, for aesthetic reasons, and also to avoid the need to replace all
our existing Mocks with a new one, following the rule that we should
not use too many classes to do the same thing (DRY).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top